Spannable String Text Diversification Settings

Keywords: Attribute

Reprinted address

What is Spannable String?

Spannable String is actually a string type, just like String. TextView can also directly set Spannable String as display text, but Spannable String can display strings in various forms and styles by using its method setSpace. It is important to specify the set interval, that is, to specify substrings within the subscript interval for strings. Format.

The setSpan(Object what, int start, int end, int flags) method requires the user to input four parameters. What is the format of the settings, such as foreground color, background color, clickable text, etc. Start represents the start subscript of the substring that needs to be formatted, and the end subscript is the same. The flags attribute is interesting. There are four attributes:

Spanned.SPAN_INCLUSIVE_EXCLUSIVE from the start subscript to the end subscript, including the start subscript
Spanned.SPAN_INCLUSIVE_INCLUSIVE from start to end, including both start and end subscriptions
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE from the start subscript to the end subscript, but does not include the start subscript and the end subscript.
Spanned.SPAN_EXCLUSIVE_INCLUSIVE from the beginning to the end, including the end

  • ForegroundColorSpan

ForegroundColorSpan, which sets foreground color for text, has the same effect as setTextColor() of TextView. The implementation method is as follows:

SpannableString spannableString = new SpannableString("Set the foreground of the text to light blue");
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
spannableString.setSpan(colorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 
textView.setText(spannableString);

The interval is set from 9 to the end of the string, which is the word "light blue" in the figure.

  • BackgroundColorSpan

BackgroundColorSpan, which sets background color, effect and setBackground() class of TextView for text, is implemented as follows:

SpannableString spannableString = new SpannableString("Set the background color of the text to light green");
BackgroundColorSpan colorSpan = new BackgroundColorSpan(Color.parseColor("#AC00FF30"));
spannableString.setSpan(colorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
  • RelativeSizeSpan

Relative SizeSpan sets the relative size of text. Based on the original text size of TextView, it sets the relative size of text. The implementation method is as follows:

SpannableString spannableString = new SpannableString("Wanzhang Tall Building Rises on the Flat Ground");

RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.2f);
RelativeSizeSpan sizeSpan02 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan sizeSpan03 = new RelativeSizeSpan(1.6f);
RelativeSizeSpan sizeSpan04 = new RelativeSizeSpan(1.8f);
RelativeSizeSpan sizeSpan05 = new RelativeSizeSpan(1.6f);
RelativeSizeSpan sizeSpan06 = new RelativeSizeSpan(1.4f);
RelativeSizeSpan sizeSpan07 = new RelativeSizeSpan(1.2f);

spannableString.setSpan(sizeSpan01, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan02, 1, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan03, 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan04, 3, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan05, 4, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan06, 5, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(sizeSpan07, 6, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
  • StrikethroughSpan

StrikethroughSpan, which sets the underlining, or deletion, for text, is implemented as follows:

SpannableString spannableString = new SpannableString("Set deletion lines for text");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

See if there is any little excitement, minute to achieve the Skycat discount concessions, there are wood?

  • UnderlineSpan

Underline Span, which underlines text, is implemented as follows:

SpannableString spannableString = new SpannableString("Underline text");
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
  • SuperscriptSpan

SuperscriptSpan, set the superscript, the specific implementation method is as follows:

SpannableString spannableString = new SpannableString("Set superscripts for text");
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
spannableString.setSpan(superscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

As can be seen from the effect chart, the text size set as the superscript is the same as the text size below. As long as we modify it slightly and set small font text as the superscript in combination with Relative SizeSpan, the exponential formula can be realized in minutes. It is no longer necessary to use 2 ^ 2 + 3 ^ 2 = 13, which lacks aesthetic mathematical formula. Is it super practical?

  • SubscriptSpan

SubscriptSpan, setting subscripts, functions similar to setting superscripts, without too much description, the specific implementation method is as follows:

SpannableString spannableString = new SpannableString("Subscription for text");
SubscriptSpan subscriptSpan = new SubscriptSpan();
spannableString.setSpan(subscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
  • StyleSpan

StyleSpan, which sets style (bold, italic) for text, is similar to TextView property textStyle. The implementation method is as follows:

SpannableString spannableString = new SpannableString("Set bold and italic style for text");
StyleSpan styleSpan_B  = new StyleSpan(Typeface.BOLD);
StyleSpan styleSpan_I  = new StyleSpan(Typeface.ITALIC);
spannableString.setSpan(styleSpan_B, 5, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);
  • ImageSpan

ImageSpan, set up text pictures, the implementation method is as follows:

SpannableString spannableString = new SpannableString("Add expressions (expressions) to the text");
Drawable drawable = getResources().getDrawable(R.mipmap.a9c);
drawable.setBounds(0, 0, 42, 42);
ImageSpan imageSpan = new ImageSpan(drawable);
spannableString.setSpan(imageSpan, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

Is this cool? Additionally, a parsing algorithm converts specific text into specific expression pictures and realizes the effect of chatting expression display in minutes.

  • ClickableSpan

ClickableSpan, set clickable text, set this attribute text can correspond to the user click event, as for click event users can customize, just like the effect map display, users can achieve click jump page effect, the specific implementation method is as follows:

SpannableString spannableString = new SpannableString("Setting Click Events for Text");
MyClickableSpan clickableSpan = new MyClickableSpan("http://www.jianshu.com/users/dbae9ac95c78");
spannableString.setSpan(clickableSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696")); 
textView.setText(spannableString);

/***************************************************************/

class MyClickableSpan extends ClickableSpan {

    private String content;

    public MyClickableSpan(String content) {
        this.content = content;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }

    @Override
    public void onClick(View widget) {
        Intent intent = new Intent(MainActivity.this, OtherActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("content", content);
        intent.putExtra("bundle", bundle);
        startActivity(intent);
    }
}

In the code, we customize the MyClickableSpan class, inherit it to ClickableSpan, and override some of its methods. ds.setUnderlineText() controls whether clickable text displays underscores. Obviously, in the code above, I chose false instead of displaying downslides. The concrete implementation method of onClick click event is written in it. As in the above code, we rewrite the onClick method of ClickableSpan to achieve the jump effect of Activity and pass the jump data.

Note: If a text using ClickableSpace wants to really click, it must set the setMovementMethod method for TextView, otherwise it does not click, and the setHighlightColor method controls the background of the click.

  • URLSpan

URLSpan, set up hyperlink text, in fact, smart guys can achieve the effect of hyperlink text when they talk about ClickableSpace, just rewrite onClick click event, and really see the source code of URLSpan. URLSpan is inherited from ClickableSpan, and as imagined, it is rewriting the parent onClick event and opening the link with the system's own browser. The present method is as follows:

SpannableString spannableString = new SpannableString("Setting hyperlinks for text");
URLSpan urlSpan = new URLSpan("http://www.jianshu.com/users/dbae9ac95c78");
spannableString.setSpan(urlSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
textView.setText(spannableString);

The source code for the URLSpanonClick event is as follows:

@Override
public void onClick(View widget) {
    Uri uri = Uri.parse(getURL());
    Context context = widget.getContext();
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Log.w("URLSpan", "Actvity was not found for intent, " + intent.toString());
    }
}

In addition, MaskFilterSpan can achieve blurring and relief effect, RasterizerSpan can achieve grating effect, because the above two use frequency is not high, and the effect is not obvious, do not elaborate, interested partners may wish to try.

SpannableStringBuilder

StringBuilder should be known by many small partners. It is very convenient to use append() method to realize string stitching. Similarly, there is Spannable String Builder in Spannable String. As the name implies, it is to achieve a splicing effect of Spannable String. It is also an append() method, which can achieve various style effects of Spannable String splicing. It is very practical.

 

 



 

Posted by ctcp on Sat, 11 May 2019 02:11:57 -0700