setting iOSButton widths via Caption / char count


I’m new to Xojo, beginning (tinkering) stage of creating an app.

I want to set the width of iOSButtons according to the width of their Captions.
I can’t see from the iOS Autolayout documentation that there is an easy way of doing this, but maybe I’m missing something?

Specifically what I want to do is to use a Text array to draw a sequence of Buttons left-to-right and justified across a View or Container… ie. with proportional spacing between them but (reasonably) consistent leading and trailing edges (based on the Caption length) within each Button.

If I wasn’t working within iOS, I think I could use Caption character count to do this. In other words, I would create a String array, derive a PushButton width from each String (a set number of points for maximum-width characters eg. ‘W’ or ‘M’ as a specified constant x the character count + leading edge constant + trailing edge constant), then plug the obtained values into Autolayout rules (again via an array or reference table) and draw the PushButtons in their left-to-right sequence.

However as I understand it iOSButton.Caption only takes a Text value… and method len( ) won’t take Text as data type, will only take a String.

So is there a way to obtain an iOSButton Caption’s character count? Or some other work-around with len ( ) or another function?

Or (even better) is there some way to achieve the above ‘sizing-according-to-caption’ using the existing iOS AutoLayout rules?
The rules enable an iOSButton to be specified in relation to the properties of other iOS objects… as far as I can understand, pretty much any drawing property except the caption of the iOSButton itself… ?

thanks Andrew

its not “working with iOS”… becuase you can do what you want with iOS (and even get the actual pixel stringwidth)… its is the limitations that Xojo requires that makes somethings difficult if not impossible.

Another option (still requires a Declare, I believe) is to do the opposite… have the text fit the button, not have the button fit the text.

Ok I should re-phrase… is there a way to do this (have Button dimensions derived from its text) within Xojo, ie any method or function applicable to Xojo’s iOSButton?
Or can this only really be done within Xcode directly? If so, it’s probably beyond my current skill level…

I could create buttons then the text to fit them, but I guess my ultimate aim would be to incorporate a button ‘builder’ or additor into the app, so the end user can easily create and insert buttons of their own choosing… hence my interest in buttons fitting text, using the len function or some equivalent.

Take a look at: specifically the button extension method AdjustsFontSizeToFitWidthXC

That’s great, will do. Thanks

but this changes the TEXTSIZE not the BUTTONSIZE

To change the button size to fit the text google says you can do this:

[code]declare sub sizeToFit lib “UIKit” selector “sizeToFit” (obj_id a ptr)

If you change the caption, call it again and it will resize again. But it will only be resized at the time of the call.