1. Black Friday Sale - 50% off

    B4A Standard for $29 instead of $59!
    Buy Now
    Dismiss Notice

B4A Class SizeToFit routines (MeasureStringWidth, MeasureStringHeight, MeasureMultilineStringHeight)

Discussion in 'Additional libraries, classes and official updates' started by Misterbates, Sep 19, 2017.

  1. Misterbates

    Misterbates Active Member Licensed User

    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)
     

    Attached Files:

    Last edited: Oct 17, 2017
    stanks, fredo, JohnC and 13 others like this.
  2. Misterbates

    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: Sep 20, 2017
  3. peacemaker

    peacemaker Well-Known Member Licensed User

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

    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 :)
     
    peacemaker likes this.
  5. Misterbates

    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
     
    peacemaker likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice