【问题标题】:How to get name of the font applied on the textview如何获取应用于文本视图的字体名称
【发布时间】:2016-06-08 07:52:51
【问题描述】:
Textview label = (TextView) findViewById(R.id.item_title);
label.setText("Solve My Issue !");
Log.d("TAG","Font-Family : "+ String.valueOf(label.getTypeface()));

当我看到日志时它返回Font-Family : android.graphics.Typeface@7f37f870

如何知道字体家族的名称?有可能吗?

【问题讨论】:

  • getTypeface() 方法返回 Typeface 类对象检查并使用其中的方法从字体访问 attr
  • 我想你错过了链接请分享它
  • 我是否有可能得到字体的确切名称

标签: android android-layout android-fonts


【解决方案1】:

getTypeface()方法返回label的Typeface,而这个Tyepface实例是Map<String, Typeface> sSystemFontMap的值,是Typeface的静态字段。所以现在你得到了值,通过反射你可以得到映射sSystemFontMap,然后想找到key,也就是字体的名字。

protected Map<String, Typeface> getSSystemFontMap() {
    Map<String, Typeface> sSystemFontMap = null;
    try {
        //Typeface typeface = Typeface.class.newInstance();
        Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
        Field f = Typeface.class.getDeclaredField("sSystemFontMap");
        f.setAccessible(true);
        sSystemFontMap = (Map<String, Typeface>) f.get(typeface);
        for (Map.Entry<String, Typeface> entry : sSystemFontMap.entrySet()) {
            Log.d("FontMap", entry.getKey() + " ---> " + entry.getValue() + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sSystemFontMap;
}

private static List<String> getKeyWithValue(Map map, Typeface value) {
    Set set = map.entrySet();
    List<String> arr = new ArrayList<>();
    for (Object obj : set) {
        Map.Entry entry = (Map.Entry) obj;
        if (entry.getValue().equals(value)) {
            String str = (String) entry.getKey();
            arr.add(str);
        }
    }
    return arr;
}

我测试过,列表arr包含以下字符串

sans-serif
tahoma
arial
helvetica
verdana

这并不奇怪,因为Android系统使用的字体与上述​​五个名称相同。 (可能各个系统版本有些差异,更多信息请到/system/etc/fonts.xml

<family name="sans-serif">
    <font weight="100" style="normal">Roboto-Thin.ttf</font>
    <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
    <font weight="300" style="normal">Roboto-Light.ttf</font>
    <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
    <font weight="400" style="normal">Roboto-Regular.ttf</font>
    <font weight="400" style="italic">Roboto-Italic.ttf</font>
    <font weight="500" style="normal">Roboto-Medium.ttf</font>
    <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
    <font weight="900" style="normal">Roboto-Black.ttf</font>
    <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
    <font weight="700" style="normal">Roboto-Bold.ttf</font>
    <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
</family>
<!-- Note that aliases must come after the fonts they reference. -->
<alias name="sans-serif-thin" to="sans-serif" weight="100" />
<alias name="sans-serif-light" to="sans-serif" weight="300" />
<alias name="sans-serif-medium" to="sans-serif" weight="500" />
<alias name="sans-serif-black" to="sans-serif" weight="900" />
<alias name="arial" to="sans-serif" />
<alias name="helvetica" to="sans-serif" />
<alias name="tahoma" to="sans-serif" />
<alias name="verdana" to="sans-serif" />

从这里你可以看到。 sans-serif,tahoma,arial,helvetica,verdana 是一回事。字体家族的不同名称sans-serif

【讨论】:

  • it return me sans-serif, tahoma, arial, helvetica, verdana 那么它到底是什么意思呢?
  • /system/etc/fonts.xml中,Android系统定义这五个名字指的是'Roboto'字体。所以总结一下,字体是Roboto
  • 有更简单的方法吗?正如我在 Typeface 的官方文档中看到的那样,不幸的是,没有名为 Typeface.NAME 或 Typeface.getName() 的属性或方法
  • 谢谢!这很好用,但有一些警告。有什么办法可以解决这些警告吗? 1. Typeface.class.getDeclaredField("sSystemFontMap") 无法解析字段'sSystemFontMap'; 2. 未经检查的强制转换:在 (Map) f.get(typeface);
猜你喜欢
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2012-08-06
  • 2014-04-25
  • 2012-05-03
  • 2011-01-14
相关资源
最近更新 更多