Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom font Spannable Typeface Span

Tags:

I am using Helvetica fonts throughout my application. Currently I am creating the fonts separately from assets. So I have say these three

HelveticaNeue = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeue.ttf"); HelveticaNeueBold = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBold.ttf"); HelveticaNeueBoldItalic = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBoldItalic.ttf"); 

Everything works great when I use one typeface for one TextView. However, I need to use a spannable

Spannable WordtoSpan = new SpannableString("This text should be normal, but this needs to be bold, and normal");     WordtoSpan.setSpan(new TypefaceSpan("bold"), 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

And I want to have part of the string be HeleveticaNeueBold and some be just HelveticaNeue. I think what is needed is to indicate to Android that the 3 fonts above are actually related so that it can switch them nicely. Looking for any other way of doing this as well.

like image 256
Leo Avatar asked Apr 01 '11 22:04

Leo


People also ask

What is Spannable string in Android?

Text styling is one of the important aspects when it comes to enhancing the UI of an Android application. In Android, we can change the size, color, weight, style, etc of a text and make the text more attractive and appealing.

What is the default text font in Android Studio?

"Roboto is a sans-serif typeface family developed by Google as the system font for its mobile operating system Android."


1 Answers

You are going to want to write a custom TypefaceSpan. The good news is that they aren't difficult. See here for an example.

like image 120
Jim Baca Avatar answered Sep 19 '22 18:09

Jim Baca