[PART SOLUTION FOUND] WordWrap issues using net.flashpunk.graphics.Text, Wrapping sometimes done when not needed


#1

I’m using the Text object to display rich text but sometimes it looks like my text is being wrapped even though there is plenty of space left on the line. It looks like if I had written a "\n " at the end of my text string, creating a new empty line.

This happens only when wordWrap is set to true (and I do need word wrapping), and then only when the text width is at a certain, but seemingly random, value. My workaround for a while has been to detect unnecessary wrapping and in those cases add an extra whitespace to the end of my string. However, only one space character doesn’t always work and I now need a more proper fix.

For reference, this is how I create my Text object:

textLabel = new Text(text, 10, 0, { font:"FontOne", size:fontsize, align:"center", wordWrap:false, resizable:false, width:width - 20, height:height, color:fontColorN, alpha:alpha } );

textLabel.setStyle("fff", { color:0xffffff } );

textLabel.richText = textLabel.text;

textLabel.y = (height - textLabel.textHeight)/2; //Vertical center alignment

Any ideas?


#2

I found a workaround. Instead of focusing on what was messing up, the textLabel.textHeight property, I turn off wordWrap by default. Then I check for if the textLabel.textWidth is greater than the width of the box I can display text in, iff it is, I turn on word wrap.

if (textLabel.textWidth > width - 10) { textLabel.wordWrap = true; }

This works well for when text needs 1 line and a 2nd one is added, but it should not work if the problem occurs when text needs 2 lines and a 3rd one is added etc. However, this solves my issues for now.