Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Last active December 18, 2015 16:19
Show Gist options
  • Save keyboardr/5810385 to your computer and use it in GitHub Desktop.
Save keyboardr/5810385 to your computer and use it in GitHub Desktop.
Quick utils for simple Spannable styling
public class StylableString extends SpannableString {
public StylableString(CharSequence source) {
super(source);
}
public StylableString setForegroundColor(int color) {
ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
setSpan(colorSpan, 0, length(), 0);
return this;
}
public StylableString setBackgroundColor(int color) {
BackgroundColorSpan colorSpan = new BackgroundColorSpan(color);
setSpan(colorSpan, 0, length(), 0);
return this;
}
public StylableString setAbsoluteSizePx(int size) {
AbsoluteSizeSpan span = new AbsoluteSizeSpan(size);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setAbsoluteSizeDp(int size) {
AbsoluteSizeSpan span = new AbsoluteSizeSpan(size, true);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setRelativeSize(float size) {
RelativeSizeSpan span = new RelativeSizeSpan(size);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setScaleX(float proportion) {
ScaleXSpan span = new ScaleXSpan(proportion);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setUrl(String url) {
URLSpan span = new URLSpan(url);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setOnClick(final Runnable runnable) {
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
runnable.run();
}
};
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setTypeFace(String family) {
TypefaceSpan span = new TypefaceSpan(family);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setStyle(int style) {
StyleSpan span = new StyleSpan(style);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setTextAppearance(Context context, int appearance) {
TextAppearanceSpan span = new TextAppearanceSpan(context, appearance);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setTextAppearance(Context context, int appearance,
int colorList) {
TextAppearanceSpan span = new TextAppearanceSpan(context, appearance,
colorList);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setTextAppearance(String family, int style, int size,
ColorStateList color, ColorStateList link) {
TextAppearanceSpan span = new TextAppearanceSpan(family, style, size,
color, link);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setAlignment(Layout.Alignment align) {
AlignmentSpan.Standard span = new AlignmentSpan.Standard(align);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setMaskFilter(MaskFilter filter) {
MaskFilterSpan span = new MaskFilterSpan(filter);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString setRasterizer(Rasterizer r) {
RasterizerSpan span = new RasterizerSpan(r);
setSpan(span, 0, length(), 0);
return this;
}
public StylableString makeStrikethrough() {
StrikethroughSpan span = new StrikethroughSpan();
setSpan(span, 0, length(), 0);
return this;
}
public StylableString makeSubscript() {
SubscriptSpan span = new SubscriptSpan();
setSpan(span, 0, length(), 0);
return this;
}
public StylableString makeSuperscript() {
SuperscriptSpan span = new SuperscriptSpan();
setSpan(span, 0, length(), 0);
return this;
}
public StylableString makeUnderline() {
UnderlineSpan span = new UnderlineSpan();
setSpan(span, 0, length(), 0);
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment