Использование собственного шрифта в Android TextView с использованием XML - PullRequest
91 голосов
/ 17 февраля 2012

Я добавил файл пользовательских шрифтов в папку assets / fonts. Как мне использовать его из моего XML?

Я могу использовать его из кода следующим образом:

TextView text = (TextView) findViewById(R.id.textview03);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
text.setTypeface(tf);

Разве я не могу сделать это из XML, используя атрибут android:typeface="/fonts/Molot.otf"?

Ответы [ 10 ]

44 голосов
/ 19 марта 2015

Краткий ответ: Нет. Android не имеет встроенной поддержки для применения пользовательских шрифтов к текстовым виджетам через XML.

Однако есть обходной путь, который не очень сложно реализовать.

First

Вам нужно определить свой собственный стиль. В папке / res / values ​​откройте / создайте файл attrs.xml и добавьте объект, который может быть объявлен как:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FontText">
        <attr name="typefaceAsset" format="string"/>
    </declare-styleable>
</resources>

Второй

Если вы хотите часто использовать этот виджет, вам следует настроить простой кэш для загруженных Typeface объектов, поскольку загрузка их из памяти на лету может занять некоторое время. Что-то вроде:

public class FontManager {
    private static FontManager instance;

    private AssetManager mgr;

    private Map<String, Typeface> fonts;

    private FontManager(AssetManager _mgr) {
        mgr = _mgr;
        fonts = new HashMap<String, Typeface>();
    }

    public static void init(AssetManager mgr) {
        instance = new FontManager(mgr);
    }

    public static FontManager getInstance() {
        if (instance == null) {
            // App.getContext() is just one way to get a Context here
            // getContext() is just a method in an Application subclass
            // that returns the application context
            AssetManager assetManager = App.getContext().getAssets();
            init(assetManager);
        }
        return instance;
    }

    public Typeface getFont(String asset) {
        if (fonts.containsKey(asset))
            return fonts.get(asset);

        Typeface font = null;

        try {
            font = Typeface.createFromAsset(mgr, asset);
            fonts.put(asset, font);
        } catch (Exception e) {

        }

        if (font == null) {
            try {
                String fixedAsset = fixAssetFilename(asset);
                font = Typeface.createFromAsset(mgr, fixedAsset);
                fonts.put(asset, font);
                fonts.put(fixedAsset, font);
            } catch (Exception e) {

            }
        }

        return font;
    }

    private String fixAssetFilename(String asset) {
        // Empty font filename?
        // Just return it. We can't help.
        if (TextUtils.isEmpty(asset))
            return asset;

        // Make sure that the font ends in '.ttf' or '.ttc'
        if ((!asset.endsWith(".ttf")) && (!asset.endsWith(".ttc")))
            asset = String.format("%s.ttf", asset);

        return asset;
    }
}

Этот файл позволит вам использовать расширения .ttc, но он не проверен.

Третий

Создайте новый класс, который подклассов TextView. Этот конкретный пример учитывает определенную гарнитуру XML (bold, italic и т. Д.) И применяет ее к шрифту (при условии, что вы используете файл .ttc).

/**
 * TextView subclass which allows the user to define a truetype font file to use as the view's typeface.
 */
public class FontText extends TextView {
    public FontText(Context context) {
        this(context, null);
    }

    public FontText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FontText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        if (isInEditMode())
            return;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FontText);

        if (ta != null) {
            String fontAsset = ta.getString(R.styleable.FontText_typefaceAsset);

            if (!TextUtils.isEmpty(fontAsset)) {
                Typeface tf = FontManager.getInstance().getFont(fontAsset);
                int style = Typeface.NORMAL;
                float size = getTextSize();

                if (getTypeface() != null)
                    style = getTypeface().getStyle();

                if (tf != null)
                    setTypeface(tf, style);
                else
                    Log.d("FontText", String.format("Could not create a font from asset: %s", fontAsset));
            }
        }
    }
}

Наконец

Замените экземпляры TextView в вашем XML на полное имя класса. Объявите свое собственное пространство имен так же, как и пространство имен Android. Обратите внимание, что «typefaceAsset» должен указывать на файл .ttf или .ttc, содержащийся в каталоге / assets.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.FontText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a custom font text"
        custom:typefaceAsset="fonts/AvenirNext-Regular.ttf"/>
</RelativeLayout>
27 голосов
/ 19 февраля 2013

Вот пример кода, который делает это.У меня есть шрифт, определенный в статической конечной переменной, а файл шрифта находится в каталоге активов.

public class TextViewWithFont extends TextView {

    public TextViewWithFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(MainActivity.typeface);
    }

    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setTypeface(MainActivity.typeface);
    }

    public TextViewWithFont(Context context) {
        super(context);
        this.setTypeface(MainActivity.typeface);
    }

}
13 голосов
/ 19 декабря 2013

Создайте свой обычный TextView принадлежащий шрифту, который вы хотите использовать. В этом классе я использую статическое поле mTypeface для кэширования гарнитуры (для повышения производительности)

public class HeliVnTextView extends TextView {

/*
 * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
 */
private static Typeface mTypeface;

public HeliVnTextView(final Context context) {
    this(context, null);
}

public HeliVnTextView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

public HeliVnTextView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);

     if (mTypeface == null) {
         mTypeface = Typeface.createFromAsset(context.getAssets(), "HelveticaiDesignVnLt.ttf");
     }
     setTypeface(mTypeface);
}

}

В XML-файле:

<java.example.HeliVnTextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        ... />

В классе Java:

HeliVnTextView title = new HeliVnTextView(getActivity());
title.setText(issue.getName());
11 голосов
/ 15 апреля 2014

Activity реализует LayoutInflater.Factory2, который предоставляет обратные вызовы для каждого созданного представления. Можно оформить TextView с помощью пользовательского атрибута Family font, загрузить шрифты по требованию и автоматически вызывать setTypeface для экземпляров текста.

К сожалению, из-за архитектурных взаимоотношений экземпляров Inflater относительно Activity и Windows, самый простой подход к использованию пользовательских шрифтов в Android - это кэширование загруженных шрифтов на уровне приложений.

Пример базы кода здесь:

https://github.com/leok7v/android-textview-custom-fonts

  <style name="Baroque" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#F2BAD0</item>
    <item name="android:textSize">14pt</item>
    <item name="fontFamily">baroque_script</item>
  </style>

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:custom="http://schemas.android.com/apk/res/custom.fonts"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
  >
  <TextView
    style="@style/Baroque"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/sample_text"
  />

результат в

enter image description here

7 голосов
/ 05 марта 2016

ОБНОВЛЕНИЕ: https://github.com/chrisjenx/Calligraphy представляется превосходное решение для этого.


Может быть, вы можете использовать отражение, чтобы вставить / взломать ваш шрифт в статический список доступных шрифтов при создании приложения? Мне интересны отзывы других, если это действительно, очень плохая идея или это отличное решение - кажется, это будет одна из этих крайностей ...

Мне удалось добавить свой пользовательский шрифт в список системных шрифтов с собственным именем семейства шрифтов, а затем указать это имя семейства пользовательских шрифтов ("brush-script") в качестве значения android: FontFamily на стандартном TextView работал на моем LG G4 под управлением Android 6.0.

public class MyApplication extends android.app.Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        Typeface font = Typeface.createFromAsset(this.getResources().getAssets(),"fonts/brush-script.ttf");
        injectTypeface("brush-script", font);
    }

    private boolean injectTypeface(String fontFamily, Typeface typeface)
    {
        try
        {
            Field field = Typeface.class.getDeclaredField("sSystemFontMap");
            field.setAccessible(true);
            Object fieldValue = field.get(null);
            Map<String, Typeface> map = (Map<String, Typeface>) fieldValue;
            map.put(fontFamily, typeface);
            return true;
        }
        catch (Exception e)
        {
            Log.e("Font-Injection", "Failed to inject typeface.", e);
        }
        return false;
    }
}

В моем макете

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fancy Text"
    android:fontFamily="brush-script"/>
7 голосов
/ 29 августа 2012

Не очень хорошая идея использовать пользовательские шрифты в xml из-за этого факта то есть вы должны сделать это программно, чтобы избежать утечки памяти!

3 голосов
/ 15 ноября 2017

Я знаю, что это старый вопрос, но я нашел гораздо более простое решение.

Сначала объявите ваш TextView в xml как обычно.Поместите ваш шрифт (TTF или TTC) в папку активов

app \ src \ main \ assets \

Затем просто установите гарнитуру для текстового представления в вашем onCreateМетод.

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);    

    TextView textView = findViewById(R.id.my_textView);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "fontName.ttf");
    textView.setTypeface(typeface);
}

Готово.

3 голосов
/ 30 мая 2017

Создайте папку шрифтов в ресурсах и добавьте туда все необходимые шрифты.

public class CustomTextView extends TextView {
    private static final String TAG = "TextView";

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        String customFont = a.getString(R.styleable.CustomTextView_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String fontName) {
        Typeface typeface = null;
        try {
            if(fontName == null){
                fontName = Constants.DEFAULT_FONT_NAME;
            }
            typeface = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + fontName);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }
        setTypeface(typeface);
        return true;
    }
}

и добавить декларацию в attrs.xml

<declare-styleable name="CustomTextView">
      <attr name="customFont" format="string"/>
</declare-styleable>

, а затем добавьте свой пользовательский шрифт, как

app:customFont="arial.ttf"
0 голосов
/ 17 октября 2018

Лучшее решение - использовать (наконец-то) введенную Google встроенную функцию пользовательских шрифтов в XML. Но вы должны настроить API 26. Он поддерживает API 16 +

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

0 голосов
/ 23 мая 2016

вместо xmlns: custom = "schemas.android.com/tools";вы должны использовать: xmlns: custom = "schemas.android.com/apk/res-auto";для того, чтобы использовать настраиваемые атрибуты.Я сделал это изменение, и оно работает сейчас.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...