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. 检测粗体和斜体支持
- 2. 的UILabel字体:粗体和斜体
- 3. 更改字体样式(粗体,斜体,粗体斜体)在C#
- 4. ImageMagick:粗体和斜体字体?
- 5. 使字体斜体和粗体
- 6. 如何从字体面板(NSFontPanel)中检索字体样式(粗体,斜体,粗斜体)和颜色?
- 7. 如何在freetype中使用粗体和斜体绘制字体
- 8. @字体面不斜体/粗体
- 9. 如何确定Windows操作系统字体是否支持粗体或斜体
- 10. Reportlab pdfgen支持粗体truetype字体
- 11. 用PIL画粗体/斜体文字?
- 12. CSS跨浏览器字体支持 - 粗体和斜体不起作用
- 13. 如何将粗体和斜体字体应用于NSAttributedString?
- 14. 使文本粗体和斜体
- 15. EM如何在Flex中嵌入字体和粗体字体?
- 16. Ghostscript粗体字体
- 17. Html在文本视图中用粗体和斜体的不同字体
- 18. 如何在Qt中设置字体粗体,斜体和下划线?
- 19. safari css粗体字体太粗体
- 20. 加粗和斜体
- 21. 在Eclipse中禁用粗体和斜体
- 22. 仅当Webfont不支持字符时才使用字体粗体
- 23. 如何在RichTextBox中保留文本格式,如粗体,斜体,字体颜色,字体大小等?
- 24. 更改checkboxGroupInput标签的字体标记(即粗体,斜体)
- 25. 如何为托管在云中的字体定义粗体,斜体?
- 26. ncurses支持斜体?
- 27. 定义CSS @字体脸部粗体斜体
- 28. htmlText不显示粗体或斜体字体
- 29. 从Edittext粗体和非粗体中制作选定的文字
- 30. 使用网页字体时,字体粗体和粗体是否有区别?
不起作用?它怎么不起作用? –