How to support italic font and bold font for Chinese

My Android app will use Chinese. The regular font is fine, but italic and bold do not work.

So which font files should I use for Chinese italics and bold?

+4
source share
2 answers

I assume that you are using TextView to display Chinese words.

If you want any words in TextView to be in bold or italics, that would be easy. Just use

 testView.getPaint().setFakeBoldText(true); 

to make all the words bold.

For italics use:

 testView.getPaint().setTextSkewX(-0.25f); 

However, if you want some words to be bold or italics. You can usually set StyleSpan in a specific range of your Spannable , but it does not work with the Chinese word.

Therefore, I suggest you create an extends StyleSpan class

 public class ChineseStyleSpan extends StyleSpan{ public ChineseStyleSpan(int src) { super(src); } public ChineseStyleSpan(Parcel src) { super(src); } @Override public void updateDrawState(TextPaint ds) { newApply(ds, this.getStyle()); } @Override public void updateMeasureState(TextPaint paint) { newApply(paint, this.getStyle()); } private static void newApply(Paint paint, int style){ int oldStyle; Typeface old = paint.getTypeface(); if(old == null)oldStyle =0; else oldStyle = old.getStyle(); int want = oldStyle | style; Typeface tf; if(old == null)tf = Typeface.defaultFromStyle(want); else tf = Typeface.create(old, want); int fake = want & ~tf.getStyle(); if ((want & Typeface.BOLD) != 0)paint.setFakeBoldText(true); if ((want & Typeface.ITALIC) != 0)paint.setTextSkewX(-0.25f); //The only two lines to be changed, the normal StyleSpan will set you paint to use FakeBold when you want Bold Style but the Typeface return say it don't support it. //However, Chinese words in Android are not bold EVEN THOUGH the typeface return it can bold, so the Chinese with StyleSpan(Bold Style) do not bold at all. //This Custom Class therefore set the paint FakeBold no matter typeface return it can support bold or not. //Italic words would be the same paint.setTypeface(tf); } } 

Set this range to your Chinese words, and I should work. Remember that it is installed only in Chinese words. I have not tested it, but I can imagine that a set of fakes on fat English characters would be very ugly.

+4
source

I suggest you not use bold and italic fonts when displaying Chinese text.

Bold is likely to distort the text, and italics will only artificially distort the text.

+1
source

Source: https://habr.com/ru/post/1382859/


All Articles