Graphics.StringHeight wrong!

I’m trying to adjust the height of a Label control in a window based on the amount of text needing to be displayed. Problem is, as soon as the label contains more than one line of text, it starts getting chopped off, with the effect getting more and more pronounced the more lines there are. So, obviously, the height returned is wrong, and is wrong for each line so the error increases.

The code I’m using, which is modeled after some code from TT’s FlowLayouter, looks like this:

[code] dim g as Graphics

if pMeasurementPicture = nil then
pMeasurementPicture = new Picture(2, 2, 32)
end if
g = pMeasurementPicture.Graphics

g.TextFont = theLabel.TextFont
g.TextUnit = theLabel.TextUnit
g.TextSize = theLabel.TextSize
g.Bold = theLabel.Bold
g.Italic = theLabel.Italic
g.Underline = theLabel.Underline

theLabel.Height = g.StringHeight(theLabel.Text, theLabel.Width)[/code]

What am I doing wrong? Or is StringHeight simply broken?

String height matches how drawstring measures & draws multiple lines and that is NOT exactly the same as how a multiline label will draw (as you’ve found)

I just ran into this myself in the IDE and conversations with framework engineers basically came to the conclusion that labels do their own thing and what we can control we do and make them the same but we don’t control labels etc where thats OS defined behavior.
Multi line labels have a slightly different metric for interline spacing, leading and lord knows what else - since its not documented by the vendors - than does ho we draw using graphics.drawstring (which matched Carbons behavior on OS X)

What I found I had to do to be close - really close - was scale by 1.2

My code ended up much like yours but the last line was

       theLabel.Height = g.StringHeight(theLabel.Text, theLabel.Width) * 1.2

You may want to fiddle a bit more & see if you can get a better factor but that’s what I came to.

That makes sense… how Xojo draws text will not necessarily be the same as the way the OS draws the same text in a control. (It would sure be nice if support for auto layout had been added by now… I see it’s been “coming real soon” for a while now.)

Your scaling factor seems to work well in my tests. The purist in me doesn’t like it, but I think that’s the best option until auto layout makes an appearance.

Thanks for your help!

Autolayout will be in iOS first
Not sure about the exact plans or timings beyond that