Custom fonts for TextView based on languages inside String

人盡茶涼 提交于 2019-12-06 05:44:21

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/26903712/custom-fonts-for-textview-based-on-languages-inside-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!