I use Paint.breakText()
to get the linecount. But it seems that it won't work for '\\n'.
text = "a\n";
int lineCount = 0;
int index = 0;
int length = text.length();
//maxWidthPX is 240
while(index < length - 1) {
index += paint.breakText(text, index, length, true, maxWidthPX, null);
lineCount++;
}
It was supposed that lineCount is 2. But it turn out that lineCount is 1. Shouldn't it be 2? Cause text contain a new line separator : '\\n'.
According to the official document paint.breakText() requires following parameters:
public int breakText(CharSequence text, int start, int end, boolean measureForwards, float maxWidth, float[] measuredWidth)
So, it breaks the character sequence from start to end position given by you where you wrote:
paint.breakText(text, index, length, true, maxWidthPX, null);
where, end = length
That's why, it always executes once!
I have edited your code:
text = "a\n";
int lineCount = 0;
int index = 0;
int length = text.length();
//maxWidthPX is 240
while(index <= length - 1) {
index += paint.breakText(text, index, index + 1, true, maxWidthPX, null);
lineCount++;
}
And, found lineCount is 2.
At last, I found a solution by using String split()
String[] textStr = text.split("\\n");
int allLineCount = 0;
for (String textTmp : textStr) {
int lineCount = 0;
int index = 0;
int length = textTmp.length();
while (index < length - 1) {
index += paint.breakText(textTmp, index, length, true, maxWidthPX, null);
lineCount++;
}
allLineCount += lineCount;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.