Legacy

[안드로이드 스튜디오 정리#19] drawText, breakText

세민짱 2022. 3. 1. 15:39
반응형

이 게시물은 다음 링크를 참조하여 학습했습니다.

 

Paint  |  Android Developers

android.net.wifi.hotspot2.omadm

developer.android.com

 

Android Center text on canvas

I'm trying to display a text using the code below. The problem is that the text is not centered horizontally. When I set the coordinates for drawText, it sets the bottom of the text at this positio...

stackoverflow.com

 

완벽한 텍스트 세로 중앙정렬을 위하여

VCNC 에서는 개발이 어느정도 마무리가 되면 기획/UX/개발 팀이 모두 모여 실제 구현물을 가지고 테스트를 해봅니다. 엣지 케이스들에서 어떻게 작동하는지, 실제 해보니 어떤 점이 불편했는지 를

www.rajin.me

 

Android 101: Typography

One way or another, every developer involves with text and some of us has to go deeper to create some custom drawings. In order to make it…

proandroiddev.com

 

Details of drawtext() and text center scheme of Android canvas - Develop Paper

There are three ways to draw text in custom view //Class I public void drawText (String text, float x, float y, Paint paint) public void drawText (String text, int start, int end, float x, float y, Paint paint) public void drawText (CharSequence text, int

developpaper.com

 

[Android] StaticLayout을 이용해서 Canvas에 텍스트를 MulitLine으로 만들어보자!!

여행기 정보를 PDF로 옮길때 텍스트 부분을 canvas.drawText를 이용해서 그려줬는데 밑 사진 처럼 MultiLine이 되지 않고 가로로 쭉~ 이어져서 모든 내용을 볼 수가 없었다.. 글자가 전체 Layout의 폭을 넘

minf.tistory.com

 

텍스트 그리기(drawText)

Paint 설정 Paint 객체는 onDraw시마다 생성하지 않고, 필드등의 전역자원으로 선언해두고 생성자에서 ...

blog.naver.com

 

안드로이드 Paint 클래스의 breakText 메소드 - 텟짱의 개발일지

텍스트의 넓이를 측정하여 지정된 넓이를 초과하지 않는 텍스트의 길이를 알려줍니다. 이를 텍스트의 줄바꿈이나 말줄임 등에 활용할 수 있습니다.

tetzzang.com

 

이번 게시물은 drawText에 관한 내용이다.

text로 입력받은 값을 커스텀한 템플릿을 제공하여 깔끔하게 PDF로 받을 수 있는 기능을 구현하고자 했었다.

Text를 입력하는데 내가 생각하는 좌표에 텍스트가 입력되지 않는 결과가 발생했고, drawText의 기준점이 내가 생각한거랑 다르다는 결론을 얻었다.

 

1. drawText의 좌표

Android의 Text에서 제공하는 Y축 관련 좌표는 위 사진과 같다.

Text를 입력할 때는 Baseline의 왼쪽지점을 기준으로 Text를 입력하게 되는데, 나는 기준 좌표가 Top의 왼쪽이라 생각해서 발생한 오류였다.

아래 코드처럼 drawText의 y좌표를 변경해준 후 내가 원하는 위치에 텍스트를 출력할 수 있었다.

1
2
3
4
fun drawText(paintNumber:Int, str: String){
        ypos -= paints.get(paintNumber).fontMetrics.top
        canvas.drawText(str, xpos, ypos, paints.get(paintNumber))
    }
cs

 

2. Text가 끊기는 문제

그런데 drawText에서는 자동으로 줄바꿈을 해주지 않기 때문에 아래처럼 텍스트가 PDF의 지정된 크기를 넘어서 입력되는 상황이 발생했다.

구글링을 하니 다 StaticLayout에 대한 설명만 있었다.

내가 구현하고자 하는 기능은 A4용지 크기에 텍스트가 넘어가면 자동으로 페이지를 넘겨줘야 했기 때문에 텍스트를 자를 수 없는 StaticLayout은 적절하지 않았다.

그래서 찾은게 breakText이다.

 

Paint의 breakText는 문자열이 지정한 길이보다 넘을 시 잘리는 위치를 반환해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fun splitText(str:String):String{
        var mStr = str
        var result:String = ""
        var end:Int = 0
        var mPaint = tv_test.paint
 
        end = mPaint.breakText(mStr, true, 100F, null)
 
        while(end>0){
            result += mStr.substring(0,end) + '\n'
            mStr = mStr.substring(end)
            end = mPaint.breakText(mStr, true, 100F, null)
        }
 
 
        return result
    }
cs
breakText 메서드에 대한 정보는 아래 링크에 있다.
 

Paint  |  Android Developers

android.net.wifi.hotspot2.omadm

developer.android.com

 

반응형