본문 바로가기
개발의 기록/Android

안드로이드에서 노토폰트 적용하기

by prographer J 2015. 8. 3.
728x90

Google에서 Adobe와 합작하여 한,중,일 폰트 Noto snas cjk font라는 이름으로 내 놓았다. 

http://googledevkr.blogspot.kr/2014/07/cjkfont.html


이 폰트를 안드로이드에 적용 하기 위해서는 Textview를 커스터 마이징을 해야 하는 약간의 작업이 필요하다. 


노토폰트 공식 사이트(http://www.google.com/get/noto/#sans-korea)에서 확인해보면 Noto Sans CJK라는 글꼴이 보이며 용량은 무려 86mb에 이른다.


이 폰트중 Regular폰트만 apk에 포함해서 빌드 할경우 대략 16mb 정도로 apk용량이 늘어나게 된다 ㄷㄷ

그래서 이 폰트는 직접 사용 할 수없고 경량화 된 폰트를 사용 하면 되는데


http://theeluwin.github.io/NotoSansKR-Hestia/


여기에서 경량화된 폰트를 얻을 수 있다. 



우선 폰트를 얻었다면, 안드로이드스튜디오에 assets폴더를 생성하여 경량화 폰트를 복사한다.




아래 코드는 gist에서 얻을 수 있다.


https://gist.github.com/Prographer/e32700cc4215cd0c37db


그리고 widget이라는 package를 하나 생성하고, NotoTextView.java를 생성한다.


NotoTextView.java에는

package com.prographer.widget;


import android.annotation.TargetApi;

import android.content.Context;

import android.graphics.Typeface;

import android.os.Build;

import android.util.AttributeSet;

import android.widget.TextView;


/**

 * Created by PrographerJ on 2015-07-17.

 * Notofont 설정

 */

public class NotoTextView extends TextView {

    public NotoTextView(Context context) {

        super(context);

        setType(context);

    }


    public NotoTextView(Context context, AttributeSet attrs) {

        super(context, attrs);

        setType(context);

    }


    public NotoTextView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        setType(context);

    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

    public NotoTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

        super(context, attrs, defStyleAttr, defStyleRes);

        setType(context);

    }


    private void setType(Context context) {

        //asset에 폰트 복사

        //NotoSnat 경령화된 폰트 위치: https://github.com/theeluwin/NotoSansKR-Hestia

        this.setTypeface(Typeface.createFromAsset(context.getAssets(), "NotoSansKR-Regular.otf"));

    }

}



사용법은 기존 TextView와 동일하게 사용이 가능하다.

https://gist.github.com/Prographer/e32700cc4215cd0c37db#file-activity_main-xml


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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.ucellslab.videoceans.activity.SearchResultsActivity">


    <com.prographer.widget.NotoTextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />


  

</RelativeLayout>


간단하게 Notofont를 적용하여 CustomVIew를 만들어 봤다.


그럼 이만~

728x90

댓글