Search code examples
androidandroid-custom-viewandroid-fonts

Custom fonts for TextView based on languages inside String


I have two font ttf files that must be applied on a TextView based on languages inside String. So e.g. consider this sample text:

hey what's up ضعيف

I can just apply a typeface span based on language but it requires custom markup in every string that is fetched from our server e.g.

 <ttf1>hey what's up <ttf1><ttf2>ضعيف</ttf2>

And parsing every String at run time will give a performance hit. Is there any other approach to achieve this?

For start lets say I need to do this just for direction of text i.e. RTL and LTR so in above example English is LTR and Arabic is RTL. Will this be any different?

I have tried merging those two font files but there are line height issues and if I fix it for one font file it gets broken for other file.


Solution

  • I found a more elegant solution than manual markup with help of someone:

    String paragraph = "hey what's up ضعيف";
    int NO_FLAG = 0;
    Bidi bidi = new Bidi(paragraph, NO_FLAG);
    int runCount = bidi.getRunCount();
    for (int i = 0; i < runCount; i++) {
        String ltrtl = bidi.getRunLevel(i) % 2 == 0 ? "ltr" : "rtl";
        String subString = paragraph.substring(bidi.getRunStart(i), bidi.getRunLimit(i));
        Log.d(">>bidi:" + i,  subString+" is "+ltrtl);
    }
    

    prints:

    hey what's up is ltr

    ضعيف is rtl

    So now one can easily build TypefaceSpan or MetricAffectingSpan based on language direction like this:

    SpannableString spanString = new SpannableString(paragraph);
    for (int i = 0; i < runCount; i++) {
        Object span = bidi.getRunLevel(i) % 2 == 0 ? ltrFontSpan : rtlFontSpan;
        spanString.setSpan(span, bidi.getRunStart(i), bidi.getRunLimit(i), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    }
    textView.setText(spanString);