- set plain text
- set foreground color for spans of text
- set background color for spans of text
- set hyperlink and color for spans of text
- set style for spans of text like bold、italic and so on
- set typeface for spans of text
- set underline for spans of text
- set strikethrough for spans of text
- set absolute size for spans of text
- set relative size for spans of text
- set clickable for spans of text
- set image for TextView
-
Gradle
compile 'com.aspook.supertextview:library:1.0.0'
-
Declare SuperTextView in the xml as follows or initialize it by code.
<com.aspook.library.SuperTextView android:id="@ id/tv_super" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
-
In the Activity ,you can use SuperTextView like this:
SuperTextView mSuperTextView = (SuperTextView) findViewById(R.id.tv_super);
// 为了更好看到效果,给SuperTextView设置一个背景色
mSuperTextView.setBackgroundColor(Color.LTGRAY);
String text = getResources().getString(R.string.test_string);
mSuperTextView.setText(text);
// 为某段文本设置前景色
mSuperTextView.setForegroundColorSpan(Color.BLUE, 24, 28);
// 为某段文本设置背景色
mSuperTextView.setBackgroundColorSpan(Color.RED, 38, 42);
// 为某段文本设置超链接,还可配置超链接文本的颜色
mSuperTextView.setURLSpan("http://aspook.com", 64, 66);
mSuperTextView.setURLSpan("tel:18888888888", 72, 74);
mSuperTextView.setURLSpan("mailto:[email protected]", 80, 82);
mSuperTextView.setURLSpan("geo:31.231365,121.492885", 88, 90);
mSuperTextView.setURLSpan("sms:18888888888", 96, 98);
mSuperTextView.setLinkTextColor(Color.BLUE);// 设置超链接的颜色
// 为某段文本设置粗体效果
mSuperTextView.setStyleSpan(Typeface.BOLD, 104, 108);
// 为某段文本设置倾斜效果
mSuperTextView.setStyleSpan(Typeface.ITALIC, 114, 118);
// 为某段文本设置加粗倾斜效果
mSuperTextView.setStyleSpan(Typeface.BOLD_ITALIC, 124, 130);
// 为某段文本设置特殊字体
mSuperTextView.setTypefaceSpan("cursive", 143, 150);
// 为某段文本设置下划线
mSuperTextView.setUnderlineSpan(159, 162);
// 为某段文本设置删除线
mSuperTextView.setStrikethroughSpan(171, 174);
// 为某段文本设置绝对字体大小
mSuperTextView.setAbsoluteSizeSpan(90, false, 192, 207);
// 为某段文本设置相对字体大小
mSuperTextView.setRelativeSizeSpan(0.5f, 227, 243);
// 为某段文本设置点击响应
mSuperTextView.setClickableSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "these text was clicked", Toast.LENGTH_SHORT).show();
}
}, 253, 259);
// 为SuperTextView设置表情图片
mSuperTextView.appendImageSpan(this, R.drawable.emoji_love);
mSuperTextView.appendImageSpan(this, R.drawable.emoji_grief_a);
mSuperTextView.appendImageSpan(this, R.drawable.emoji_kiss);
mSuperTextView.appendImageSpan(this, R.drawable.emoji_naughty);
This project is subject to the Apache License, Version 2.0.