본문 바로가기

Android

[Android]커스텀 폰트 간단 적용법

반응형

안드로이드 오레오 이전에는 커스텀 폰트를 적용하기 위한 다양한 방법들이 존재했습니다.


하지만 오레오 버전에서부터는 간단한 방법으로 원하는 폰트를 적용할 수 있습니다.




우선 리소스 폴더에 font 폴더를 생성한 후 사용하고자 하는 폰트들을 넣어둡니다.


그리고 아래처럼 레이아웃에 적용하시거나 코드 작성으로 직접 적용하시면 됩니다.


1
2
3
4
5
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/notosanskr_medium_hestia"
        android:text="Lorem ipsum" />
cs


1
2
3
Typeface typeface = getResources().getFont(R.font.notosanskr_medium_hestia);
textView.setTypeface(typeface);
cs


정말 간단하게 폰트를 바꿀 수 있습니다.


여기서 조금 더 나아가 자신만의 Font-Family를 만들어서 사용할 수도 있습니다.




우선 font 폴더에 font resource file을 생성한 후 아래와 같은 형식으로 작성합니다.


저는 my_font_family라는 이름으로 생성하였습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
 
    <!-- normal -->
    <font
        android:font="@font/notosanskr_demilight_hestia"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/notosanskr_demilight_hestia"
        app:fontStyle="normal"
        app:fontWeight="400" />
 
    <!-- italic -->
    <font
        android:font="@font/android_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/android_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />
 
    <!-- bold -->
    <font
        android:font="@font/notosanskr_medium_hestia"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/notosanskr_medium_hestia"
        app:fontStyle="normal"
        app:fontWeight="700" />
 
</font-family>
cs

저는 각각 normal에는 notosans demilight, italic에는 android italic, bold에는 notosans medium 폰트를 적용시키겠습니다.

그리고 마찬가지로 레이아웃에 적용을 시켜줍니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"

        android:textStyle="normal"
        android:fontFamily="@font/my_font_family"

        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/textView2" />
 
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"

        android:textStyle="italic"
        android:fontFamily="@font/my_font_family"

        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="@+id/textView3" />
 
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Lorem ipsum"

        android:textStyle="bold"
        android:fontFamily="@font/my_font_family"

        app:layout_constraintBottom_toBottomOf="@+id/textView7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toTopOf="@+id/textView7" />
cs




이처럼 안드로이드 오레오부터는 폰트 적용이 이처럼 손쉽기 때문에 커스텀폰트를 사용하시는 분들은


타겟 SDK를 안드로이드 오레오 이상으로 해주시는걸 추천드립니다.



다음 포스팅은 매번 TextView 및 EditText에 fontfamily를 적용하지 않고


style.xml을 이용하여 조금 더 간단하게 일괄 적용할 수 있는 방법에 대해 알아보겠습니다.



반응형