본문으로 바로가기

안드로이드 개발시 텍스트뷰(TextView)의 문자크기(textSize)를 동적으로 변경해주고 싶은 경우가 있는데요. 두가지 함수를 이용해 구현합니다.

(setTextSize()함수: 내용은 아래 [참고]항목을 확인해주시기 바랍니다)

 

 

 

1. textView의 getTextSize() 함수로 현재 값을 받습니다

(리턴값은 float임을 유의합니다).

2. textView의 setTextSize(int unit, float size)함수로 문자크기를 지정합니다

(unit값 설정에 유의합니다. 원하는 대로 텍스트 사이즈가 변경되지 않아 어려움이 있으신 분들은

아래 [참고] 부분의 함수에서 정의된 대로 픽셀 Unit을 설정해주면 해결되실 것 같습니다)

 

float currentSize = textView.getTextSize();

float newSize;

//TODO : 원하는 newSize  ​ (사용자의 입력값을 받거나, 버튼의 이벤트를 받는 등등 원하는 방법을 구현하는 항목입니다)

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);







[참고] 

1. setTextSize()함수

void

setTextSize(int unit, float size)

Set the default text size to a given unit and value.

void

setTextSize(float size)

Set the default text size to the given value, interpreted as "scaled pixel" units.


2. TypedValue

https://developer.android.com/reference/android/util/TypedValue.html#q=setTextSize