在我的ListField中,我希望能够采用任何给定的长字符串,并且只能够将第一行换行到屏幕的宽度内,并且只剩下其余的字符串并在下面显示并省略掉其余的字符串。现在,这就是我用来检测画图调用中包裹的方法:
int totalWidth = 0;
int charWidth = 0;
int lastIndex = 0;
int spaceIndex = 0;
int lineIndex = 0;
String firstLine = "";
String secondLine = "";
boolean isSecondLine = false;
for (int i = 0; i < longString.length(); i++){
charWidth = Font.getDefault().getAdvance(String.valueOf(longString.charAt(i)));
//System.out.println("char width: " + charWidth);
if(longString.charAt(i) == ' ')
spaceIndex = i;
if((charWidth + totalWidth) > (this.getWidth()-32)){
//g.drawText(longString.substring(lastIndex, spaceIndex), xpos, y +_padding, DrawStyle.LEFT, w - xpos);
lineIndex++;
System.out.println("current lines to draw: " + lineIndex);
/*if (lineIndex = 2){
int idx = i;
System.out.println("first line " + longString.substring(lastIndex, spaceIndex));
System.out.println("second line " + longString.substring(spaceIndex+1, longString.length()));
}*/
//firstLine = longString.substring(lastIndex, spaceIndex);
firstLine = longString.substring(0, spaceIndex);
//System.out.println("first new line: " +firstLine);
//isSecondLine=true;
//xpos = 0;
//y += Font.getDefault().getHeight();
i = spaceIndex + 1;
lastIndex = i;
System.out.println("Rest of string: " + longString.substring(lastIndex, longString.length()));
charWidth = 0;
totalWidth = 0;
}
totalWidth += charWidth;
System.out.println("total width: " + totalWidth);
//g.drawText(longString.substring(lastIndex, i+1), xpos, y + (_padding*3)+4, DrawStyle.ELLIPSIS, w - xpos);
//secondLine = longString.substring(lastIndex, i+1);
secondLine = longString.substring(lastIndex, longString.length());
//isSecondLine = true;
}
现在,这确实可以很好地包装任何给定的字符串(假设y值正确偏移,并且仅在字符串宽度超过屏幕宽度之后才绘制文本,然后再绘制其余的字符串),但是,每次尝试要获得前两行,如果它超出了两行,它总是最后返回字符串的后两行。既然我不了解新想法,是否有更好的方法来做这种事情?
最佳答案
我不确定我是否理解您的问题,但这听起来像是您需要一种自动换行算法,该算法在两行之后就放弃了。如果真是这样,那么这应该有望使您指明正确的方向。
void wrapString(String inputString, int width, Font font){
String line1;
String line2;
if (font.getAdvance(inputString) <= width){
line1 = inputString;
line2 = "";
}
else{
int charsInLine1 = countCharsInLine(inputString, 0, inputString.length()-1, width, font);
int lineBreak = inputString.lastIndexOf(' ', charsInLine1);
if (lineBreak == -1)
lineBreak = charsInLine1;
line1 = inputString.substring(0, lineBreak);
line2 = inputString.substring(lineBreak+1, inputString.length());
if (font.getAdvance(line2) > width){
int charsInLine2 = countCharsInLine(line2, 0, inputString.length()-1, width - font.getAdvance("..."), font);
line2 = line2.substring(0, charsInLine2) + "...";
}
}
System.out.println("line1: " + line1);
System.out.println("line2: " + line2);
}
int countCharsInLine(String str, int min, int max, int width, Font font) {
if (min >= max)
return max;
int guess = min + (max - min) / 2;
int advance = font.getAdvance(str, 0, guess);
if (advance < width)
return countCharsInLine(str, guess+1, max, width, font);
else if (advance > width)
return countCharsInLine(str, min, guess-1, width, font);
else
return guess;
}
关于java - Blackberry ListField文本包装-仅两行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2187245/