# Sizing a control to fit all text

**URL:** <https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980>\
**Category:** General\
**Created:** [October 8, 2026, 8:28pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980 "2026-10-08T20:28:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_Antrim](https://forum.xojo.com/user_avatar/forum.xojo.com/clifford_antrim/32/507_2.png) [@Clifford\_Antrim](https://forum.xojo.com/u/Clifford_Antrim)\
**Post date:** [October 8, 2026, 8:28pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/1 "2026-10-08T20:28:17Z")

</div>

I have several blocks of text that can each be long or short or anywhere in between. I want to display them as a list, but with the ability to show each block as a small 2 or 3 line preview or expand it to show the entire text. I want the text to be selectable, but not editable.

I am currently using a DesktopLabel to display the text, and calculating the height of the label for the preview is easy and accurate, but getting the correct height for the expanded mode is proving to be frustrating. I am currently using this function:

```xojo
Public Function Height(Extends val As String, wrapWidth As Double, fontName As String, fontSize As Double, isBold As Boolean = False, isItalic As Boolean = False) As Double
Dim p As New Picture(1, 1)

p.Graphics.FontName = fontName
p.Graphics.FontSize = fontSize
p.Graphics.Bold = isBold
p.Graphics.Italic = isItalic

Return p.Graphics.TextHeight(val, wrapWidth)

End Function

```

The calculated height is generally too short, but by how much seems to change with how many EndOfLines there are and how long the text is. I haven’t really found a pattern. I’m thinking that the label has some padding that the Graphics object doesn’t, and it could possibly wrap different than a graphics object?

Any ideas? Or another approach? A different control to display the text in, but still be able to do the expand/collapse action?

---

<div class="post-metadata">

**Author:** ![Clifford\_Antrim](https://forum.xojo.com/user_avatar/forum.xojo.com/clifford_antrim/32/507_2.png) [@Clifford\_Antrim](https://forum.xojo.com/u/Clifford_Antrim)\
**Post date:** [October 8, 2026, 8:52pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/2 "2026-10-08T20:52:47Z")

</div>

I have been playing with accounting for padding in the label and it seems to be mostly accurate now - it will sometimes be an extra line too tall but it’s not cutting off the text anymore.

I get the width of the label and subtract from that the padding, and then pass that number into the height function as the wrap width. Then I add the padding to the calculated height and set Label.Height to that. FWIW a padding value of 16 is working pretty well.

I am still open to other suggestions if there is a better way.

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [October 9, 2026, 5:57am UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/3 "2026-10-09T05:57:26Z")

</div>

Maybe the [FlowLayout](https://www.einhugur.com/Html/FlowLayout/index.html) Plugin by @Björn_Eiríksson from [Einhugur](https://www.einhugur.com/) can help with this?

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [October 9, 2026, 6:39am UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/4 "2026-10-09T06:39:46Z")

</div>

> [@Clifford\_Antrim](#):
>
> The calculated height is generally too short, but by how much seems to change with how many EndOfLines there are and how long the text is.

There’s a bug already reported: [https://tracker.xojo.com/xojoinc/xojo/-/work\_items?sort=created\_date&state=opened&search=TextHeight&first\_page\_size=20&show=eyJpaWQiOiI2OTY1NyIsImZ1bGxfcGF0aCI6Inhvam9pbmMveG9qbyIsImlkIjo2MTcyN30%3D](https://tracker.xojo.com/xojoinc/xojo/-/work_items?sort=created_date&state=opened&search=TextHeight&first_page_size=20&show=eyJpaWQiOiI2OTY1NyIsImZ1bGxfcGF0aCI6Inhvam9pbmMveG9qbyIsImlkIjo2MTcyN30%3D)

It’s in the category of those framework bugs that last for several years and are impacting basic functionalities, like DesktopTextArea.LineNumAtCharPos which has never been reliable since at least Cocoa was a target: [https://tracker.xojo.com/xojoinc/xojo/-/work\_items/43874](https://tracker.xojo.com/xojoinc/xojo/-/work_items/43874)  
I’m really tired of them.

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [October 9, 2026, 7:58am UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/5 "2026-10-09T07:58:35Z")

</div>

> [@Arnaud\_N](#):
>
> [https://tracker.xojo.com/xojoinc/xojo/-/work\_items/43874](https://tracker.xojo.com/xojoinc/xojo/-/work_items/43874)

There is this one as well:  
[https://tracker.xojo.com/xojoinc/xojo/-/work\_items/51461](https://tracker.xojo.com/xojoinc/xojo/-/work_items/51461)

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [October 9, 2026, 7:59am UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/6 "2026-10-09T07:59:10Z")

</div>

What platforms are you targetting?

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [October 9, 2026, 8:01am UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/7 "2026-10-09T08:01:06Z")

</div>

Yes, I chose to not list them all, to not overload the forum software 😅

---

<div class="post-metadata">

**Author:** ![Clifford\_Antrim](https://forum.xojo.com/user_avatar/forum.xojo.com/clifford_antrim/32/507_2.png) [@Clifford\_Antrim](https://forum.xojo.com/u/Clifford_Antrim)\
**Post date:** [October 9, 2026, 12:24pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/8 "2026-10-09T12:24:23Z")

</div>

> [@kevin\_g](#):
>
> What platforms are you targeting?

MacOS and Windows.

---

<div class="post-metadata">

**Author:** ![Clifford\_Antrim](https://forum.xojo.com/user_avatar/forum.xojo.com/clifford_antrim/32/507_2.png) [@Clifford\_Antrim](https://forum.xojo.com/u/Clifford_Antrim)\
**Post date:** [October 9, 2026, 12:25pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/9 "2026-10-09T12:25:20Z")

</div>

> [@Sascha\_S](#):
>
> Maybe the [FlowLayout](https://www.einhugur.com/Html/FlowLayout/index.html) Plugin by @Björn_Eiríksson

I just got Bjorn’s plugins earlier this week - I give that a try and see if it’s any better.

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [October 9, 2026, 2:02pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/10 "2026-10-09T14:02:07Z")

</div>

OK.

On macOS you should be able to do this using Declares or the MBS plugins. Here is the code we use in a v1 API Label control subclass method.

```auto
    Dim nsTextFieldObj As NSTextFieldMBS
    Dim nsTextFieldCellObj As NSTextFieldCellMBS
    Dim boundsNSRectObj As NSRectMBS
    Dim boundsCellSizeObj As NSSizeMBS
    
    'retrieve the nstextfield and nstextfieldcell for the control
    nsTextFieldObj = Self.NSTextFieldMBS
    nsTextFieldCellObj = nsTextFieldObj.cell
    
    
    'define the bounds which is the nstextfield bounds width (which may be different to the control width) and a large height
    boundsNSRectObj = New NSRectMBS
    boundsNSRectObj.Width = nsTextFieldObj.bounds.Width
    boundsNSRectObj.Height = 99999
    
    
    'retrieve the cell size that fits the text and update the height
    boundsCellSizeObj = nsTextFieldCellObj.cellSizeForBounds(boundsNSRectObj)
    
    Self.Height = Ceil(boundsCellSizeObj.Height)

```

On MS-Windows, things are more difficult. Xojo Graphics use Direct2D (DirectWrite) but the UI controls when not using WinUI use GDI / GDI+ which are two completely different technologies. We solved this using the Win32 API via Declares. Unfortunately, I cannot share this code but you should start by looking at the DrawTextW API call.

---

<div class="post-metadata">

**Author:** ![Clifford\_Antrim](https://forum.xojo.com/user_avatar/forum.xojo.com/clifford_antrim/32/507_2.png) [@Clifford\_Antrim](https://forum.xojo.com/u/Clifford_Antrim)\
**Post date:** [October 9, 2026, 2:27pm UTC](https://forum.xojo.com/t/sizing-a-control-to-fit-all-text/88980/11 "2026-10-09T14:27:18Z")

</div>

@kevin_g Thanks for the code and pointing me in the right direction for Windows. I appreciate it!
