Reputation: 23638
I have a custom fonts BebasNeue.otf
which i want to set in the strings variables. I have searched a lot but did not get any solution.
I know we can set the custom fonts in views like textView.setTypeface(font);
. But now i want to set the fonts to the Strings
which i have as below :
String Name,Contact;
Name="ABCD";
Contact="97696538";
This all the strings contains some values. Now i want to set the custom fonts to all the strings which is below:
Typeface fontString = Typeface.createFromAsset(m_context.getAssets(),
"BebasNeue.otf");
Can any one tell me how can i set the above fonts in my Strings variable.
Any help will be appreciated .
Thanks.
Upvotes: 3
Views: 7065
Reputation: 801
Maybe this can help.
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);
}
}
And use it like this
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Upvotes: 0
Reputation: 23638
I have resolved my issue as below and with the help of the comments provided by @ASP
.
Typeface fontString = Typeface.createFromAsset(m_context.getAssets(),
"BebasNeue.otf");
Paint paint = new Paint();
paint.setTypeface(fontString);
mCanvas.drawText("ID :" + m_sId, dw - 450, dh - 350, paint);
Thanks all for helping me and quickly.
Upvotes: 5