Resize label height to fit wrapped text.

P

.pd.

Hello,

Since CF doesn't support the 3-arg MeasureString(), how can I dynamically
set the height of a label (I'm doing a tooltip kind of control) to fit
its text?

I have this so far:

Label text = new Label();
int tipWidth = 180;
[...]
SizeF sz = e.Graphics.MeasureString(Tip, font);
int lines = Convert.ToInt32(sz.Width / tipWidth) + 1;

int height = Convert.ToInt32(lines * (sz.Height));
text.Size = new Size(tipWidth, height);
// add a pixel all round to fit the border on
this.Size = new Size(tipWidth + 2, height + 2);

The problem with this is that it doesn't take into account space wasted
by word-wrapping.

How can I calculate how many extra lines I need? Is there some way of
extracting n pixels from a drawn string and working out if the last word
is complete?

Thanks for any help,

..pd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top