앱 Application/안드로이드 Android

Q. 안드로이드Android TextView에 HTML 코드 사용시 사용 가능한 태그 종류는?

Tap to restart 2020. 11. 20. 17:00
반응형

사용 가능한 태그 종류는 다음과 같다고 한다.


(출처: 안드로이드 개발자 가이드)

지원되는 HTML 요소:
    굵은꼴: <b>, <em>
    기울임꼴: <i>, <cite>, <dfn>
    텍스트 25% 확대: <big>
    텍스트 20% 축소: <small>
    글꼴 속성 설정: <font face=”font_family“ color=”hex_color”>. 사용 가능한 글꼴 집합은 monospace, serif, sans_serif 등이 있습니다.
    고정 폭 글꼴 집합: <tt>
    취소선: <s>, <strike>, <del>
    밑줄: <u>
    위첨자: <sup>
    아래첨자: <sub>
    글 머리꼴: <ul>, <li>
    줄바꿈: <br>
    분할: <div>
    CSS 스타일: <span style=”color|background_color|text-decoration”>
    문단: <p dir=”rtl | ltr” style=”…”>


글자 크기 조절 테스트

저 내용을 대충 훑어만 보고 글자 크기도 자유자재로 조절되겠지 생각해서 해보면 글자 크기 조절은 안 된다.

<font size="1.2em">Hello World!</font>

이런 코드는 아무리 해도 작동하지 않는다.

 

CSS 스타일도 사용가능하다고 나오지만, font-size는 안 된다.

String htmlText = "<span style=\"font-size: 2em\">Hello World!</span><br>Hello!";
textView.setText(Html.fromHtml(htmlText), TextView.BufferType.SPANNABLE);

위 코드처럼 작성해서 실행하면

 

Hello World!

Hello!

 

로 나오길 기대하지만 그냥 둘다 글자 크기가 똑같게 나온다. 아래처럼 말이다.

 

Hello World!

Hello!

 

android.text.Html.java 소스 코드 보기

android.text.Html.java 소스 코드를 보면 왜 안 되는지 알 수 있다.

span 관련 코드에서 style을 처리해주지 않는다.

 

 

span으로 설정할 수 있는 것은 color, background color, text-decoration 3가지다.

 

font size가 안 되는 이유는?

    private void startFont(Editable text, Attributes attributes) {
        String color = attributes.getValue("", "color");
        String face = attributes.getValue("", "face");
        if (!TextUtils.isEmpty(color)) {
            int c = getHtmlColor(color);
            if (c != -1) {
                start(text, new Foreground(c | 0xFF000000));
            }
        }
        if (!TextUtils.isEmpty(face)) {
            start(text, new Font(face));
        }
    }

소스코드에 size 관련 코드가 없다.

글꼴 속성 설정: <font face=”font_family“ color=”hex_color”>

face와 color만 된다.

 

<big> <small> 태그는 작동

문장에서 글자 일부만 바꾸고 싶다면 <big> <small> 을 활용해야 한다.

else if (tag.equalsIgnoreCase("big")) {
	end(mSpannableStringBuilder, Big.class, new RelativeSizeSpan(1.25f));
} else if (tag.equalsIgnoreCase("small")) {
	end(mSpannableStringBuilder, Small.class, new RelativeSizeSpan(0.8f));
}

이렇게 실제 코드가 있다.

 

해결책은?

해결책은 <big><small>을 겹쳐서 쓰든가,

아니면 HTML을 상속받아서 추가로 구현하든가이다.

추가로 구현하기는 귀찮다.

 

적당히

<big><big>Are you OK?</big></big><br>

<small><small>Maybe...</small></small>

이런 식으로 써야할 거 같다.

반응형