Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

android - How to get name of the font applied on the textview

Textview label = (TextView) findViewById(R.id.item_title);
label.setText("Solve My Issue !");
Log.d("TAG","Font-Family : "+ String.valueOf(label.getTypeface()));

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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() + "
");
        }
    } 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;
}

I tested it, and the List arr contain below String

sans-serif
tahoma
arial
helvetica
verdana

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" />

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...