I want to calculate how many lines some text needs to fit into a fixed-width TextView
. And I used Paint.breakText
for this:
int maxWidth = WIDTH_IN_PIXELS;
int index = 0;
int linecnt = 0;
while (true) {
float[] measuredWidth = new float[1];
int advances = labelView.getPaint().breakText(text, index, text.length(), true, maxWidth, measuredWidth);
linecnt += 1;
index += advances;
if(index>=desc.length()) break;
}
However, the calculated linecnt
is always one less than what is really needed. After further investigation I find the API breaks text at arbitrary points within words, but when TextView layout text, it never breaks a word:
// textBreak:
Hello Wo
rld
// Real layout:
Hello
World
How do I get breakText to respect word wrapping? Or is there any other way to do this right?
p.s. I know I can always move back to the beginning of word within the loop, but that looks hacky and doesn't apply to every language. I am sure Android has some elegant way to do this, but I don't know the API/Flag to use.
p.p.s Sometimes TextView uses dash (-) to break a long word across lines, the rules for this is unclear.