-- Android
텍스트 관련 함수 정리
어린왕자악꿍
2015. 7. 30. 10:18
미루고만 있었던 텍스트뷰어를 만들기 위해 조사했었었는데 텍스트뷰어에 쓰이지는 않았으나 유용한 것들이라 생각하여 여기에 정리해둔다.
1. 텍스트에 맞는 적당한 가로,세로 구하기
- View의 onMeasure 함수에서 특정 텍스트에 맞는 가로세로 크기를 구할 때 유용함.
Layout layout = makeLayout("ABCD");
int desiredWidth = layout.getWidth();
int desireHeight = layout.getHeight();
private Layout makeLayout(CharSequence text) {
return new StaticLayout(text, textPaint,
(int) java.lang.Math.ceil(Layout.getDesiredWidth(text, textPaint)),
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}
int desiredWidth = layout.getWidth();
int desireHeight = layout.getHeight();
private Layout makeLayout(CharSequence text) {
return new StaticLayout(text, textPaint,
(int) java.lang.Math.ceil(Layout.getDesiredWidth(text, textPaint)),
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}
2. 스크린에 들어갈 텍스트의 전체 라인 수
Paint paint = new Paint(); 3. TextView에 터치 할 때 텍스트 색상을 바꾸는 방법 |