2011-11-24 419 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會非常難看。

相關問題