차근차근/HTML CSS

import 나눔고딕 , 나눔명조 로딩 느림 2

예쁜꽃이피었으면 2015. 1. 21. 15:00

[android/안드로이드] Font 에 대해 알아보아요.

http://aroundck.tistory.com/122



 안드로이드, Webview 에 Custom font 적용하기

http://aroundck.tistory.com/2107


구글링 해 본 결과, 안드로이드 2.1 버전에서는 버그로 이 방법이 적용 안 된다고 한다.


먼저 font file( TTF ) 은 asset 폴더에 넣는다.



<sample code>

private void initView(){

WebView webView = new WebView( this );

setContentView( webView );

String meta = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>";

String style =  "<style>@font-face {font-family: 'gamza';src: url('file:///android_asset/h.ttf');}body {font-family: 'gamza';}</style>"

String body = "<body><strong>감자! Strong </strong><br /><b>감자! Bold</b><br />감자! 그냥</body>";


String head = "<head>" + meta + style + "</head>";

String htmlContents = "<html>" + head + body + "</html>";

        

webView.loadDataWithBaseURL( "file:///android_asset/", htmlContents, "text/html", "utf-8", null );

}


핵심정리!


custom font 를 asset 에 두고, html page 에 이 font 를 style 로 연결시켜준다.





@font-face{

font-family: 'fontName';

src: url('file:///android_asset/fontName.ttf');

}

body{

font-family: 'fontName';

}


여기서 주의사항은 WebView 에 데이터를 연동시킬 때, font 를 불러올 context 를 맞춰줘야 한다.

즉, WebView.loadDataWithBaseURL () 의 첫번째 파라미터에 BaseURL 로 file:///android_asset/ 을 연결해줌으로서

context 를 맞춰주어야만 정상적으로 html 로딩이 가능하다.


그게 싫다면, html 파일 자체를 asset 에 동일하게 두는 방식으로 context 를 맞춰주어야 한다.




http://www.androidpub.com/1144367

저같은 경우 데이터를 HTML 내용을 String으로 전환한다음

webView.loadData(webContent.replace("</head>","<style type=\"text/css\"> body { font-family:\"맑은 고딕\"; font-size:12pt }</style></head>"),"text/html", "utf-8");

이렇게 로드했는데~


안드로이드 CustomFont 적용하기

http://osankkk.tistory.com/category/Android/UI

1) 일부분 폰트 적용. 

1. 폰트를 다운받는다.

2. 폰트를 프로젝트에 넣는다. ( 나눔고딕으로 넣었다. )

    위치 : (프로젝트)assets/fonts/NanumGothicBold.ttf

3. Typeface 생성 ( Activity 에서 )

   Typeface tf = Typeface.createFromAsset( getAssets() , "fonts/NanumGothicBold.ttf" );

   또는 context를 전달받는 형태의 유틸클래스로 만들어 놓고 쓰는게 좋다.

4.   TextView textView1 = (TextView)findViewById(R.id.textView1);

      textView1.setTypeface( tf );

끝..

2) 시스템 전역 폰트 적용. 



반응형