bluedot Posted August 22, 2014 Share Posted August 22, 2014 Hi, I've got a variable text string, what's the best method to fit it within a fixed size box ? I'm thinking of a loop to update fontsize and get the width/height until the text fit. Thanks. Link to comment Share on other sites More sharing options...
lewster32 Posted August 23, 2014 Share Posted August 23, 2014 That's basically how the routines I've seen do it - usually a while loop with some sanity checking to ensure infinite loops don't occur. Something like this maybe? (Untested!)while(text.width > box.width && text.fontSize > 0) { text.fontSize--; text.updateText();} webcaetano_ 1 Link to comment Share on other sites More sharing options...
Sebi Posted August 23, 2014 Share Posted August 23, 2014 It really depends on the use case. If the text changes often, you might want to extend the Text prototype and loop over ctx.measureText(..).width instead of drawing the text until you find a fitting size. Since the box also has a height, I would wrap at box.width and loop until the height fits into the box. (Not sure if the current version of phaser i using a pixi version with fixed wordwrap issue) Link to comment Share on other sites More sharing options...
Recommended Posts