1

I am working on a android app in which I display some text in japanies using TextView. In addition to the text, I want to show some annotations on top of certain characters in the text (ruby annotation - https://en.wikipedia.org/wiki/Ruby_character). My research did not show any inbuilt API's from Android to support ruby annotation. So I am trying custome drawText() method to write text and annotations on to a canvas. In drawText() I am writing each character to certain (x,y) position with variable font size for main text and annotation text.

Since I am determining the (x,y) position of each char, Now the dimensions of the textView became tricky. Some times, the main text maybe very large. The way I am calculating the Width and Height of textView is Width = paint.measureText(length if longest text line (In case of multiple lines rendering); Height = Number of lines * font size.

// Setting the width of TextView
@Override public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { 

paint.setTextSize(util.getTextSize());

return (int) paint.measureText(text);
}

I am having trouble in how to set the height of textView?

Manikanta
  • 223
  • 3
  • 18

2 Answers2

0

It's been about a year since I have really worked on this so I am a little rusty. As far as I know there isn't a direct way to set the height of the font in an Android TextView.

In a project I was working on before I used a loop to repeatedly measure the height until I got something that was close enough. See the setTextSizeToMatchHeight() method here.

You can look at the source code for TextView. I recall for the auto sizing methods they also used some sort of loop.

Other possible solutions:

  • If your font size is fixed in your app, you could maybe used a hardcoded font size for your Ruby text as well.
  • In my app that I linked to above, if I were to update it, I would consider converting the text to a path and then resizing the path to the height I wanted. I haven't played with it yet but I think it would be easier to resize path heights than font heights. To convert a string to a path you could use Paint.getTextPath().
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • the desiredHeight in the setTextSizeToMatchHeight is derived from Number of lines * lineHeight? Can you share some insights on how are you calculating the desiredHeight? – Manikanta Nov 14 '19 at 01:23
  • In that particular app the desired height is based on the user dragging a rectangle to a new height. – Suragch Nov 14 '19 at 12:39
  • I see, In my case. We have a TextView with padding on all sides, And The way I am calculating the Height is (number of lines of text to display) * (mTextView.getLineHeight). which is not working when it comes to single line text. Any suggestions on how to accurately or near to accurately calculate the heigh in this case ? Also could you please comment on TextView to canvas relation. Meaning is TextView is a window on top of canvas (meaning the text with in the scope of the window only visible)? Is there a way to know if canvas top left corner align with TextView top left corner? – Manikanta Nov 14 '19 at 19:35
  • @Manikanta, when I am working with custom painting I don't use a TextView at all. I either use a [lower level layout](https://stackoverflow.com/questions/41779934/how-is-staticlayout-used-in-android/41779935#41779935) or just draw everything with paint and canvas. – Suragch Nov 15 '19 at 01:24
0

I havnt figured our the answer completely. But you can set the height of the textview. Which is textView.setHeight(). Once I calculated the height of canvas, I set the same to TextView and that worked. no cliping.

Manikanta
  • 223
  • 3
  • 18