"Turn" Android TextView spannablestringbuilder text-to-picture mixed-line Color italic bold underline strikethrough

Source: Internet
Author: User
Tags gettext

Spannablestringbuilder usage:

spannablestring ss = new Spannablestring ("Red call italics strikethrough green underline Picture:.");
//color-coded text
Ss.setspan (New Foregroundcolorspan (color.red), 0, 2,
Setspan required Flag,spanned.span_exclusive_exclusive (not included before and after).
spanned.span_exclusive_exclusive);
//Tag text with hyperlinks
Ss.setspan (New Urlspan ("tel:4155551212"), 2, 5,
spanned.span_exclusive_exclusive);
//Mark text with Style (italic)
Ss.setspan (New Stylespan (Typeface.bold_italic), 5, 7,
spanned.span_exclusive_exclusive);
//Use strikethrough to mark text
Ss.setspan (New Strikethroughspan (), 7, 10,
spanned.span_exclusive_exclusive);
//Underline text with underscore
Ss.setspan (New Underlinespan (), 10, 16,
spanned.span_exclusive_exclusive);
//color-coded
Ss.setspan (New Foregroundcolorspan (Color.green), 10, 13,
spanned.span_exclusive_exclusive);
//Get drawable Resources
drawable d = getresources (). getdrawable (R.drawable.icon);
D.setbounds (0, 0, d.getintrinsicwidth (), D.getintrinsicheight ());
//Create Imagespan
Imagespan span = new Imagespan (d, Imagespan.align_baseline);
//Replace text with Imagespan
Ss.setspan (span,,, spannable.span_inclusive_exclusive);
Txtinfo.settext (ss);
Txtinfo.setmovementmethod (Linkmovementmethod.getinstance ()); Implementing scrolling of text

Parameter description:

Object What: corresponds to the various spans, which will be mentioned later;
int start: Start applying the location of the specified span, starting with index 0
int end: Ends the position where the specified span is applied, and the effect does not include this position. For example, if the number is 3 (that is, the 4th character), the 4th character will have no effect. It can be seen from the example below.
INT flags: There are four of the following values
Spannable.span_exclusive_exclusive: None before and after, that is, inserting new characters before and after a specified range does not apply a new style
Spannable.span_exclusive_inclusive: Not included before, included later. That is, the new style is applied only when a new character is inserted after the range character
Spannable.span_inclusive_exclusive: Included earlier, not included later.
Spannable.span_inclusive_inclusive: Both front and back are included.

Usually used to display text, but sometimes also need to be in the text with some pictures, such as QQ can use emoticons, such as the need for text highlighting and so on, how to do so in Android?
Remember that there is a Android.text package in Android, which provides powerful handling of the text.
Add pictures primarily with spannablestring and Imagespan classes:

drawable drawable = Getresources (). getdrawable (ID);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
//Text to be processed, [smile] is the text that needs to be substituted
spannablestring spannable = new Spannablestring (GetText (). toString () + "[Smile]");
//To make the picture replace the specified text with Imagespan
Imagespan span = new Imagespan (drawable, imagespan.align_baseline);
Start replacing, note that the 2nd and 3rd parameters indicate where to start replacing where to replace end (start and end)
The last parameter is similar to the set in mathematics, [5,12] represents from 5 to 12, including 5 but excluding 12
Spannable.setspan (span, GetText (). Length (), GetText (). Length () + "[smile]". Length (), spannable.span_inclusive_ EXCLUSIVE);
SetText (spannable);

Highlight the text you want:



public void Highlight (int start,int end) {
Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());//For variable strings
Foregroundcolorspan span=new Foregroundcolorspan (color.red);
Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);
}

Add Underline:



public void underline (int start,int end) {
Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());
Characterstyle span=new Underlinespan ();
Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);
}

Combined use:



Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());
Characterstyle span_1=new Stylespan (Android.graphics.Typeface.ITALIC);
Characterstyle span_2=new Foregroundcolorspan (color.red);
Spannable.setspan (span_1, start, end, spannable.span_exclusive_exclusive);
Spannable.setspan (span_2, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);

Case: a string with \ n line break can display 2 colors in this method



/**
* string with \ n line break can display 2 colors in this method
* @param text
* @param color1
* @param color2
* @return
*/
Public Spannablestringbuilder Highlight (String text,int color1,int color2,int fontSize) {
Spannablestringbuilder spannable=new Spannablestringbuilder (text);//For variable strings
Characterstyle span_0=null,span_1=null,span_2;
int End=text.indexof ("\ n");
if (end==-1) {//display with the first color if there is no line break
Span_0=new Foregroundcolorspan (Color1);
Spannable.setspan (span_0, 0, Text.length (), spannable.span_exclusive_exclusive);
}else{
Span_0=new Foregroundcolorspan (Color1);
Span_1=new Foregroundcolorspan (COLOR2);
Spannable.setspan (span_0, 0, end, spannable.span_exclusive_exclusive);
Spannable.setspan (span_1, End+1, Text.length (), spannable.span_exclusive_exclusive);

Span_2=new Absolutesizespan (fontSize);//font size
Spannable.setspan (Span_2, End+1, Text.length (), spannable.span_exclusive_exclusive);
}
return spannable;
}

"Turn" Android TextView spannablestringbuilder text-to-picture mixed-line Color italic bold underline strikethrough

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.