4
A
回答
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會非常難看。
相關問題
- 1. 更改字體樣式(粗體,斜體,粗體斜體)在C#
- 2. ImageMagick:粗體和斜體字體?
- 3. 使字體斜體和粗體
- 4. 如何從字體面板(NSFontPanel)中檢索字體樣式(粗體,斜體,粗斜體)和顏色?
- 5. 如何確定Windows操作系統字體是否支持粗體或斜體
- 6. 用PIL畫粗體/斜體文字?
- 7. 如何將粗體和斜體字體應用於NSAttributedString?
- 8. EM如何在Flex中嵌入字體和粗體字體?
- 9. Ghostscript粗體字體
- 10. 如何在Qt中設置字體粗體,斜體和下劃線?
- 11. safari css粗體字體太粗體
- 12. 加粗和斜體
- 13. 在Eclipse中禁用粗體和斜體
- 14. 更改checkboxGroupInput標籤的字體標記(即粗體,斜體)
- 15. htmlText不顯示粗體或斜體字體
- 16. 使用網頁字體時,字體粗體和粗體是否有區別?
- 17. 顏色和粗體文字
- 18. UITextView粗體字體sdk
- 19. Mysteriously粗體字體CSS3
- 20. Powerpoint VBA粗體字體
- 21. 如何從richtextbox中刪除任何格式(粗體,斜體,字體,字體大小)WPF
- 22. 如何告訴sIFR 3使用粗體,斜體等不同的字體?
- 23. Blackberry:LabelField文本的粗體字體
- 24. 中文字體和粗體文字行爲
- 25. 在iPhone中使用粗體重的粗體字體
- 26. 通過傳遞React.js中的值使字體粗體粗體
- 27. ScalaTags中的簡潔粗體/斜體字段落
- 28. Pdf銳利的字體樣式粗體,斜體和下劃線一起
- 29. 如何在AndEngine中使用斜體或粗體字體創建自定義字體?
- 30. 使用CGContext,我該如何使字體爲斜體還是粗體?
不起作用?它怎麼不起作用? –