2011-11-24 316 views

回答

1

我建议你不要使用加粗斜体显示中文文字时的字体。

粗体很可能会扭曲文字,而斜体只会人为地歪斜文字。

4

我假设您使用TextView来显示中文单词。

如果您希望TextView中的任何单词都是粗体或斜体,那很简单。 只需使用

testView.getPaint().setFakeBoldText(true); 

使所有粗体字。

斜体,使用:

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

但是,如果你只是想有些话是粗体或斜体。通常情况下,您可以在Spannable的特定范围上设置StyleSpan,但这不适用于中文单词。

因此,我建议你创建一个类扩展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); 
    } 
} 

设置此跨度你们中国的话,我要工作。 请注意检查它是否仅限于中文字。我还没有对它进行测试,但我可以想象,用大胆的英文字符设置fakebold会非常难看。

相关问题