【发布时间】:2012-05-18 18:09:29
【问题描述】:
我的 android 应用程序将使用中文。常规字体还可以,但是斜体和粗体就不行了。
那么对于中文的斜体和粗体我应该使用哪些字体文件呢?
【问题讨论】:
-
不起作用?怎么不行?
我的 android 应用程序将使用中文。常规字体还可以,但是斜体和粗体就不行了。
那么对于中文的斜体和粗体我应该使用哪些字体文件呢?
【问题讨论】:
我假设您使用TextView 显示中文单词。
如果您希望 TextView 中的任何单词为粗体或斜体,这很容易。 只需使用
testView.getPaint().setFakeBoldText(true);
使所有单词加粗。
斜体,使用:
testView.getPaint().setTextSkewX(-0.25f);
但是,如果您只想将某些单词设为粗体或斜体。通常你可以在Spannable的特定范围内设置StyleSpan,但它不适用于中文。
因此,我建议你创建一个类 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);
}
}
将此范围设置为您的中文单词,我应该工作。 请注意检查它仅设置在中文单词上。我没有对其进行测试,但我可以想象在粗体英文字符上设置 fakebold 会非常难看。
【讨论】:
我建议你在显示中文文本时不要使用bold和italic字体。
粗体可能会扭曲文本,而斜体只会人为地扭曲文本。
【讨论】: