Blackberry ListField Text Wrapping - 只有两行

大鹏一日同风起,扶摇直上九万里。这篇文章主要讲述Blackberry ListField Text Wrapping - 只有两行相关的知识,希望能为你提供帮助。
在我的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; }

另一答案我做了类似的事情,但它将返回所有扭曲的线条。然后,您可以使用返回向量的第一个元素并组合其余元素以获得所需的两行:
private Vector wrap (String text, int width) { Vector result = new Vector (); String remaining = text; while (remaining.length()> =0) { int index = getSplitIndex(remaining, width); if (index == -1) break; result.addElement(remaining.substring(0,index)); remaining = remaining.substring(index); if (index == 0) break; }return result; }private int getSplitIndex(String bigString, int width) { int index = -1; int lastSpace = -1; String smallString=""; boolean spaceEncountered = false; boolean maxWidthFound = false; for (int i=0; i< bigString.length(); i++) { char current = bigString.charAt(i); smallString += current; if (current == ' ') { lastSpace = i; spaceEncountered = true; }int linewidth = this.getFont().getAdvance(smallString,0,smallString.length()); if(linewidth> width) { if (spaceEncountered) index = lastSpace+1; else index = i; maxWidthFound = true; break; }}if (!maxWidthFound) index = bigString.length(); return index; }

【Blackberry ListField Text Wrapping - 只有两行】到目前为止,我仍在测试并正常工作。

    推荐阅读