【问题标题】:How to get type face style of a custom font in android?如何在android中获取自定义字体的字体样式?
【发布时间】:2015-11-24 21:27:53
【问题描述】:

使用自定义字体后,我无法通过以下方式获取文本视图的字体样式(BOLD、ITALIC、BOLD_ITALIC):

textView.getTypeface.getStyle();

实际上设置新的字体样式并不适用于 textView,虽然我们可以看到样式在视觉上发生了变化。我这样设置字体样式:

Typeface font = Typeface.createFromAsset(getAssets(), "sampleFont.ttf");
textView.setTypeface(font, Typeface.BOLD);

但是,

textView.getTypeface.getStyle();

返回 0(正常)而不是 1(粗体)! 当我尝试使用常规字体时,所有这些都会发生。

【问题讨论】:

    标签: android fonts typeface custom-font android-fonts


    【解决方案1】:

    Typeface 从 API 级别 1 开始也有 isBold()isItalic() 方法。

    或者试试这个

    if(textView.getTypeface()!=null){
       if(textView.getTypeface().getStyle()==Typeface.BOLD || textView.getTypeface().getStyle()==Typeface.ITALIC){
          //your code goes here
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多