Gutan
Gutan

Reputation: 461

Android using a special unicode character

Help folks, I am developing an app for android and need to use only 1 weird unicode character:

\u0180 - ƀ

My question is:

is it possible to simply insert this char into an existing font or any other way than to include a huge 5-10 mb font into the application that supports this unicode char? Please any ideas, any links, some directions. Thanks folks.

Upvotes: 2

Views: 1670

Answers (1)

SqueezyMo
SqueezyMo

Reputation: 1726

Keywords to google around: Typeface, MetricAffectingSpan, SpannableString.

You don't have to include the whole set of characters you don't need. You can create your own typeface containing the only character with a code of your choice. And then you can use this typeface to span strings that contain that character.

Assuming you have a TTF font file called custom_font with your character, you can put it in your assets/fonts folder and create a typeface object:

Typeface customTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/custom_font.ttf");

and then you can span your special character across the string (assuming you don't know its exact position beforehand):

SpannableStringBuilder resultSpan = new SpannableStringBuilder(yourString);

for (int i = 0; i < resultSpan.length(); i++) {
  if (resultSpan.charAt(i) == '\u0180') {
    CustomTypefaceSpan typefaceSpan = new CustomTypefaceSpan(customTypeface);
    resultSpan.setSpan(typefaceSpan, i, i + 1, 0);
  }
}

SpannableStringBuilder implements CharSequence, so your TextViews and EditTexts will gladly accept it.

You can take the implementation of CustomTypefaceSpan from this answer.

Upvotes: 2

Related Questions