Wednesday, May 25, 2011

Class for Creating Bold Text in Android

This class has saved me TONS of time ...


public class StyleableSpannableStringBuilder extends SpannableStringBuilder {

public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle c, CharSequence text) {
super.append(text);
final int startPos = length() - text.length();
setSpan(c, startPos, length(), 0);
return this;
}

public StyleableSpannableStringBuilder appendBold(CharSequence text) {
return appendWithStyle(new StyleSpan(Typeface.BOLD), text);
}

public StyleableSpannableStringBuilder appendItalic(CharSequence text) {
return appendWithStyle(new StyleSpan(Typeface.ITALIC), text);
}
}

No comments: