Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the width of the text?
#1
Hello.

I would sometimes like to dynamically set the size of the TIWListbox.

I was able to calculate the height of the ListBox1:
MyHeight  :=  round(ListBox1.Items.Count * (ListBox1.Font.Size * ListBox1.Font.PixelsPerInch/72)) +  ListBox1.Items.Count * 3;

I don't know how to calculate the width. In Delphi, canvas is used for this. But TIWListbox has no canvas.
How to calculate the width of any any row of a ListBox1?
Reply
#2
The short answer is you cannot. There is no canvas because the browser renders it and many things from plugins, zoom, and accessibility features can alter the actual size on the browser.

The only reliable way is to use JavaScript on the browser and pass the info back which requires a round trip call back to the server.

You can use auto size options if you simply want it to adjust to show all items.
Reply
#3
(02-12-2021, 04:08 PM)I.Igor Wrote: Hello.

I would sometimes like to dynamically set the size of the TIWListbox.

I was able to calculate the height of the ListBox1:
MyHeight  :=  round(ListBox1.Items.Count * (ListBox1.Font.Size * ListBox1.Font.PixelsPerInch/72)) +  ListBox1.Items.Count * 3;

I don't know how to calculate the width. In Delphi, canvas is used for this. But TIWListbox has no canvas.
How to calculate the width of any any row of a ListBox1?

You can't obtain it with 100% accuracy without code running on browser side, but you can get it close enough for most practical purposes.

Use existing TIWFont methods and properties, example:

You would get the height of a single item on the list with something like:

ListBox1.Font.PxSize (size in pixels of the font when rendered)

You can also use other methods like:

// convert any font size to pixel size
class function PtToPx(AFontSize: Integer): Integer;

// converts EM to pixels
class function EmToPx(AValue: Single; AFontSize: Integer): Integer;

e.g.:

x := TIWFont.EmToPx(1.1, 12);

x will get the size in pixels of a text which height is 1.1em, using font size 12.

with a little experimentation you can get it almost perfect
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)