B4i Library 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.
Android version is at https://www.b4x.com/android/forum/t...ingheight-measuremultilinestringheight.84069/

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.
 

Attachments

  • iSizeToFit v3.zip
    6.5 KB · Views: 137
Last edited:

JohnC

Expert
Licensed User
Longtime User
Thank you!
 

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
* 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. In IOS it makes no difference whether a label has been added to a parent; in Android it *does* make a difference, so I added MeasureExpectedSize for cross-platform portability.
 

Misterbates

Active Member
Licensed User
Uploaded v3. Bugfixes:
  • MeasureExpectedSize adjusted so that if both Width and Height are zero, it'll return the smallest values (for both) that will fit the text
 
Top