user1063675
user1063675

Reputation: 41

How to support italic font and bold font for Chinese

My android application will use Chinese. The regular font is OK, but the italic font and bold font does not work.

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

Upvotes: 4

Views: 2876

Answers (2)

Yeung
Yeung

Reputation: 2231

I assume you are using TextView to show Chinese words.

If you want the whatever words in TextView to be bold or italic, it would be easy. Just use

testView.getPaint().setFakeBoldText(true);

to make all words bold.

For italic, use:

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

However, if you only want some words to be bold or italic. Normally you can set StyleSpan on specific range of your Spannablebut it is not work on Chinese word.

Therefore, I suggest you create a class extends StyleSpan

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 span to your Chinese words and I should be work. Be aware to check it is only set on Chinese words only. I have not test on it but I can imagine that set fakebold on a bold English characters would be very ugly.

Upvotes: 4

benni_mac_b
benni_mac_b

Reputation: 8877

I'd suggest you don't use bold and italic fonts when displaying chinese text.

Bold is likely to distort the text and italic will only artificially skew the text.

Upvotes: 1

Related Questions