Alex Rivera | Logout

Calculate needed size for a TLabel

Asked 2010-04-27T13:06:33.520
24

Ok, here's the problem. I have a label component in a panel. The label is aligned as alClient and has wordwrap enabled. The text can vary from one line to several lines. I would like to re-size the height of the the panel (and the label) to fit all the text.

How do I get the necessary height of a label when I know the text and the width of the panel?

Edit
Report

2 Answers

7

You can use one line of code for this:

label.width := label.canvas.textwidth(label.caption);

or you can set the label's autosize property to true in the object inspector.

answered 2010-04-27T23:02:17.950
2

If you can align it alTop and keep AutoSize on then TLabel will auto adjust the height after settign the caption.

answered 2010-04-27T13:11:26.640

Your Answer