首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何支持中文的斜体和粗体

如何支持中文的斜体和粗体
EN

Stack Overflow用户
提问于 2011-11-24 18:01:34
回答 2查看 2.5K关注 0票数 4

我的android应用程序将使用中文。常规字体可以,但斜体字体和粗体字体不起作用。

那么,对于中文斜体和粗体,我应该使用哪些字体文件呢?

EN

回答 2

Stack Overflow用户

发布于 2012-05-18 18:09:29

我假设您正在使用TextView来显示中文单词。

如果您希望TextView中的任何单词都是粗体或斜体,这将很容易。只需使用

代码语言:javascript
复制
testView.getPaint().setFakeBoldText(true);

让所有的单词都变得粗体。

对于斜体,请使用:

代码语言:javascript
复制
testView.getPaint().setTextSkewX(-0.25f);

但是,如果您只希望某些单词是粗体或斜体的话。通常你可以在Spannable的特定范围内设置StyleSpan,但在中文word上不起作用。

因此,我建议您创建一个扩展StyleSpan的类

代码语言:javascript
复制
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);
    }
}

将此跨度设置为您的中文单词,我应该是工作。请注意,请注意它仅设置在中文单词上。我还没有测试过,但我可以想象到在粗体的英文字符上设置伪粗体将是非常丑陋的。

票数 4
EN

Stack Overflow用户

发布于 2011-11-24 18:18:34

我建议您在显示中文文本时不要使用、粗体、和斜体字体。

粗体可能会扭曲文本,而斜体只会人为地扭曲文本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8255177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档