我的android应用程序将使用中文。常规字体可以,但斜体字体和粗体字体不起作用。
那么,对于中文斜体和粗体,我应该使用哪些字体文件呢?
发布于 2012-05-18 18:09:29
我假设您正在使用TextView来显示中文单词。
如果您希望TextView中的任何单词都是粗体或斜体,这将很容易。只需使用
testView.getPaint().setFakeBoldText(true);让所有的单词都变得粗体。
对于斜体,请使用:
testView.getPaint().setTextSkewX(-0.25f);但是,如果您只希望某些单词是粗体或斜体的话。通常你可以在Spannable的特定范围内设置StyleSpan,但在中文word上不起作用。
因此,我建议您创建一个扩展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);
}
}将此跨度设置为您的中文单词,我应该是工作。请注意,请注意它仅设置在中文单词上。我还没有测试过,但我可以想象到在粗体的英文字符上设置伪粗体将是非常丑陋的。
发布于 2011-11-24 18:18:34
我建议您在显示中文文本时不要使用、粗体、和斜体字体。
粗体可能会扭曲文本,而斜体只会人为地扭曲文本。
https://stackoverflow.com/questions/8255177
复制相似问题