LoginSignup
25
18

More than 5 years have passed since last update.

Androidで標準以外のフォントを使用する

Last updated at Posted at 2017-09-22

はじめに

TextViewに標準以外のフォントを指定する場合、iOSのようにXcodeでフォントを指定する…といったことができません。
※ Android Oからは利用可能になるようです。

しかし、ライブラリを使うことで簡単に好きなフォントを利用することができます。

手順

  1. フォントファイルの準備
  2. プロジェクトへの追加
  3. ライブラリの準備と導入

1. フォントファイルの準備

好きなフォントを準備してください。例えばNoto Sansは以下から取得します。

2. プロジェクトへの追加

Android StudioのプロジェクトウインドウをProjectビューに切り替えます。app/src/main配下にassetsディレクトリを作成します。さらにfontsディレクトリを作成し、その中に準備したフォントを追加します。

screenshot_2017-06-27_18.46.06.png

3. ライブラリの準備と導入

ライブラリは以下を利用します。
chrisjenx/Calligraphy: Custom fonts in Android the easy way...

具体的な導入はREADME.mdにありますが、簡単に記載します。

ライブラリのインポート

app/build.gradleに以下を追記します。

app/build.gradle
dependencies {
    compile 'uk.co.chrisjenx:calligraphy:2.3.0'
}

初期化

利用するアクティビティで以下を追記します。

SampleActivity.java
@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

TextViewへの指定

layout/sample_text_style.xml
<TextView
    fontPath="fonts/HelveticaNeue.ttf"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="あいうえおあいうえおあいうえおあいうえお"
    />

赤い波線が出てエラーと言われますがDisable Inspectionsしてください。

イメージ

Screenshot_1498564543.png

参考

25
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
18