B4A Class SizeToFit routines (MeasureStringWidth, MeasureStringHeight, MeasureMultilineStringHeight)

Routines to adjust a label to fit its text/adjust text size to fit a label:
* SizeToFitWidth - given a width (and using the label's font), adjust the label's height
* SizeToFitHeight - given a height (and using the label's font), adjust the label's width
* SizeFontToFit - given some text for the label (and using the label's width/height), adjust the label's fontsize so that the text fills the label
* MeasureExpectedSize - Returns Width/Height/FontSize (type SizeToFit_Metrics) for a label that hasn't yet been added to a parent view
* getFontSize & setFontSize - cross-platform subs to get/set the fontsize for a label

These routines will work under Android or IOS leading to a little more portability for developers supporting Apps running on both platforms. I've not tested on B4J.
IOS version is at https://www.b4x.com/android/forum/t...ingheight-measuremultilinestringheight.84070/

They can be used in place of or in conjunction with other routines like SizeToFit, MeasureStringHeight, MeasureStringWidth, MeasureMultilineStringheight etc.

Note that the attached is not a library - it's a code module with accompanying test/demo.

Requires: StringUtils library (MeasureMultilineTextHeight), Reflector library (GetActivityBA and GetField)
 

Attachments

  • aSizeToFit v3.zip
    11.6 KB · Views: 948
Last edited:

Misterbates

Active Member
Licensed User
Uploaded v2 into first post.
Bugfixes, code cleanups (to move platform dependent stuff into utility routines) plus a few new subs:
* MeasureExpectedSize - Returns Width/Height/FontSize (type SizeToFit_Metrics) for a label that hasn't yet been added to an activity/view (using Reflector object)
* getFontSize & setFontSize - cross-platform subs to get/set the fontsize for a label

I wanted the ability to calculate layout positions *before* programmatically adding labels to activities/panels. The method used is quite expensive but as long as used sparingly should be acceptable - see https://www.b4x.com/android/forum/t...ctivity-from-a-code-module.68666/#post-471562
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Super !
Didn't you test with labels whose text was made from a CSBuilder ? That can contain images, various formatting...
 

Misterbates

Active Member
Licensed User
Apologies, I didn't test with CSbuilder text as I'm not using that in my app. The source is provided in the attached zip so that you can use it/test it/adjust it as needed. Would be interested to know what you find :)
 

Misterbates

Active Member
Licensed User
Uploaded v3. Couple of bugfixes:
  • SizeTofitWidth fixed to handle single character strings correctly.
  • MeasureExpectedSize adjusted so that if both Width and Height are zero, it'll return the smallest values (for both) that will fit the text
 

Douglas Farias

Expert
Licensed User
Longtime User
thank you for the example. this will help me alot. thx.

just a comment.
i dont know why a emoji is detecting like 2 characters lenght.
i have tested with 🤣 and this bug.

the
B4X:
If aLabel.Text.Length < 10 Then
dont catch this emoji, because this have 2 lenght.

i m just changed to <5 and now works fine.

thxx
 
Top