Skip to content

Instantly share code, notes, and snippets.

@mpaloulack
Last active August 29, 2015 13:57
Show Gist options
  • Save mpaloulack/9765287 to your computer and use it in GitHub Desktop.
Save mpaloulack/9765287 to your computer and use it in GitHub Desktop.
Android Many font in button
final SpannableString textVidOTW = new SpannableString(Html.fromHtml("Video<br/>OF THE WEEK"));
textVidOTW.setSpan(new RelativeSizeSpan(0.7f*density), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textVidOTW.setSpan(new TypefaceSpan(this, "fonts/Lighthouse_PersonalUse.ttf"), 0, 5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textVidOTW.setSpan(new RelativeSizeSpan(0.3f*density), 5, textVidOTW.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//and add class
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(
12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
String.format("%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment