Code example for SpannableStringBuilder

Methods: getSpanStart, length, removeSpan, setSpan

0
        int len = text.length();
        text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
    } 
 
    private static void endFont(SpannableStringBuilder text) {
        int len = text.length();
        Object obj = getLast(text, Font.class);
        int where = text.getSpanStart(obj);
 
        text.removeSpan(obj);
 
        if (where != len) {
            Font f = (Font) obj;
 
            if (f.mColor != null) {
                int c = -1;
 
                if (f.mColor.equalsIgnoreCase("aqua")) {
                    c = 0x00FFFF;
                } else if (f.mColor.equalsIgnoreCase("black")) {
                    c = 0x000000;
                } else if (f.mColor.equalsIgnoreCase("blue")) {
                    c = 0x0000FF;
                } else if (f.mColor.equalsIgnoreCase("fuchsia")) {
                    c = 0xFF00FF;
                } else if (f.mColor.equalsIgnoreCase("green")) {
                    c = 0x008000;
                } else if (f.mColor.equalsIgnoreCase("grey")) {
                    c = 0x808080;
                } else if (f.mColor.equalsIgnoreCase("lime")) {
                    c = 0x00FF00;
                } else if (f.mColor.equalsIgnoreCase("maroon")) {
                    c = 0x800000;
                } else if (f.mColor.equalsIgnoreCase("navy")) {
                    c = 0x000080;
                } else if (f.mColor.equalsIgnoreCase("olive")) {
                    c = 0x808000;
                } else if (f.mColor.equalsIgnoreCase("purple")) {
                    c = 0x800080;
                } else if (f.mColor.equalsIgnoreCase("red")) {
                    c = 0xFF0000;
                } else if (f.mColor.equalsIgnoreCase("silver")) {
                    c = 0xC0C0C0;
                } else if (f.mColor.equalsIgnoreCase("teal")) {
                    c = 0x008080;
                } else if (f.mColor.equalsIgnoreCase("white")) {
                    c = 0xFFFFFF;
                } else if (f.mColor.equalsIgnoreCase("yellow")) {
                    c = 0xFFFF00;
                } else { 
                    try { 
                        c = convertValueToInt(f.mColor, -1);
                    } catch (NumberFormatException nfe) {
                        // Can't understand the color, so just drop it. 
                    } 
                } 
 
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
                                 where, len,
                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } 
            } 
 
            if (f.mFace != null) {
                text.setSpan(new TypefaceSpan(f.mFace), where, len,
                             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } 
        } 
    }