공부방/Android

How to android custom font change programmatically (안드로이드 )

soycrab 2020. 9. 3. 16:05

안드로이드 Custom 폰트 변경을 코드로 할경우 아래와 같이 처리하면 된다. 

커스텀 폰트를 아래와 같이 적용 했다는 가정 하에

res/font

yoonfont.xml

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

    <font
        android:font="@font/yoon740"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/yoon740"
        app:fontStyle="normal"
        app:fontWeight="400"/>

    <font
        android:font="@font/yoon770"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/yoon770"
        app:fontStyle="normal"
        app:fontWeight="700"/>

</font-family>

 

extension.kt

fun TextView.setTextBold(isBold: Boolean) {
    this.typeface = ResourcesCompat.getFont(this.context,if(isBold) R.font.yoon770 else R.font.yoon740)
}

 

사용방법 

textView.setTextBold(true)

반응형