Permalink
Browse files

PIVOT-889: Allow newline characters in Label text to do a hard line b…

…reak

when "wrapText" is set true.

I think this is "what you would expect" in this case, so I'm not doing a
new style (even for backward compatibility).  If someone feels it is necessary
it wouldn't be hard to add such a style.


git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1435351 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent e528b4c commit 346340a5b56233a47f092410ab532c46e140236f Roger Lee Whitcomb committed Jan 18, 2013
Showing with 40 additions and 23 deletions.
  1. +40 −23 wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java
View
63 wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java
@@ -127,22 +127,29 @@ public int getPreferredHeight(int width) {
int i = 0;
while (i < n) {
char c = text.charAt(i);
- if (Character.isWhitespace(c)) {
- lastWhitespaceIndex = i;
- }
-
- Rectangle2D characterBounds = font.getStringBounds(text, i, i + 1,
- fontRenderContext);
- lineWidth += characterBounds.getWidth();
-
- if (lineWidth > width
- && lastWhitespaceIndex != -1) {
- i = lastWhitespaceIndex;
-
+ if (c == '\n') {
lineWidth = 0;
lastWhitespaceIndex = -1;
preferredHeight += lineHeight;
+ } else {
+ if (Character.isWhitespace(c)) {
+ lastWhitespaceIndex = i;
+ }
+
+ Rectangle2D characterBounds = font.getStringBounds(text, i, i + 1,
+ fontRenderContext);
+ lineWidth += characterBounds.getWidth();
+
+ if (lineWidth > width
+ && lastWhitespaceIndex != -1) {
+ i = lastWhitespaceIndex;
+
+ lineWidth = 0;
+ lastWhitespaceIndex = -1;
+
+ preferredHeight += lineHeight;
+ }
}
i++;
@@ -244,21 +251,29 @@ public void layout() {
StringCharacterIterator ci = new StringCharacterIterator(text);
while (i < n) {
char c = text.charAt(i);
- if (Character.isWhitespace(c)) {
- lastWhitespaceIndex = i;
- }
+ if (c == '\n') {
+ appendLine(text, start, i, fontRenderContext);
- Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext);
- lineWidth += characterBounds.getWidth();
-
- if (lineWidth > width
- && lastWhitespaceIndex != -1) {
- appendLine(text, start, lastWhitespaceIndex, fontRenderContext);
-
- i = lastWhitespaceIndex;
start = i + 1;
lineWidth = 0;
lastWhitespaceIndex = -1;
+ } else {
+ if (Character.isWhitespace(c)) {
+ lastWhitespaceIndex = i;
+ }
+
+ Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext);
+ lineWidth += characterBounds.getWidth();
+
+ if (lineWidth > width
+ && lastWhitespaceIndex != -1) {
+ appendLine(text, start, lastWhitespaceIndex, fontRenderContext);
+
+ i = lastWhitespaceIndex;
+ start = i + 1;
+ lineWidth = 0;
+ lastWhitespaceIndex = -1;
+ }
}
i++;
@@ -647,6 +662,8 @@ public boolean getWrapText() {
* Sets whether the text of the label will be wrapped to fit the Label's width.
* Note that for wrapping to occur, the Label must specify a preferred width or
* be placed in a container that constrains its width.
+ * Also note that newline characters (if wrapping is set true) will cause a hard
+ * line break.
*/
public void setWrapText(boolean wrapText) {
this.wrapText = wrapText;

0 comments on commit 346340a

Please sign in to comment.