如何获取应用于文本视图的字体的名称 [英] How to get name of the font applied on the textview

查看:91
本文介绍了如何获取应用于文本视图的字体的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i see the log it return Font-Family : android.graphics.Typeface@7f37f870

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

How to know the name of the font family ? Is it possible ?

推荐答案

getTypeface()方法返回label的字体,而此Tyepface实例的值是Map<String, Typeface> sSystemFontMap,这是Typeface的静态字段.因此,现在您获取了值,并通过反射可以获取地图sSystemFontMap,然后想要查找键,它恰好是字体的名称.

getTypeface() method returns the Typeface of label, while this Tyepface instance is a value of Map<String, Typeface> sSystemFontMap, which is a static field of Typeface. So now you get the value, and with reflection you can get the map sSystemFontMap, then want to find the key, which is exactly the name of font.

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包含在字符串下面

I tested it, and the List arr contain below String

sans-serif
tahoma
arial
helvetica
verdana

这并不奇怪,因为Android系统使用与上述五个名称相同的一种字体. (可能会因系统版本不同而有所差异,有关更多信息,请转到/system/etc/fonts.xml)

That's not strange, because Android system use one same font as above five names. (Maybe some differences in various system version, for more information goto /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

from this you can see. sans-serif,tahoma,arial,helvetica,verdana are same thing. different names of font family sans-serif

这篇关于如何获取应用于文本视图的字体的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆