08-01-2020 10:16 AM
Note (6-30-2021): this technique cannot be used within Power Apps applications created within Teams.
Updated on 9-13-2020: in step 3 formula, the lblMyLabel has been replaced with the Self operator; simply copy the formula to your label's Width property and it'll work fine!
I am very happy today to provide the Power Apps Community with this Auto Width Label Generator component!
This component will allow you to make your application's label controls grow or shrink according to their content. And this auto-width feature will work whatever the Power Apps font you use and whatever the font-weight of your label.
And guess what: it works for buttons as well!
Before using this component though, make sure to read the recommandations and instructions below...
How does it work?
Well, in fact it's pretty simple... A Power Apps collection named colCharsWidth lists the exact width of each and every of the 255 Latin characters from the ASCII table. Then, a simple formula placed inside the Width property of a label will calculate its size, given the characters present in its Text property.
How can this help me build my applications?
Well, for instance, it will help you with the following:
Until today, making multi-language applications was a challenge as labels content could vary in size according to the language, thus making it hard, for instance, to have nice toolbars that would adjust themselves to their content.
Before, a toolbar would have fixed-sized labels just like this:
Some of you decided probably to maintain the size of each label within the list of translations for the various labels. But what a pain to maintain!
Now, using this little trick of mine, your toolbar will just look like a normal toolbar, whatever the language:
Breadcrumbs and tabs
Using this component, you will be able to have breadcrumbs where the nice little chevrons between each level will move over according to the content of each label at each level, assuming they are positionned using relative positionning of course.
Likewise, tabs will adjust their sizes whatever their content or language.
How do I use it?
From there on, the component is pretty self-explanatory, but I'll detail it here anyway:
And BOOOOM it's done! Your labels have now auto-width! 😃
Why create a new app dedicated to that component?
Because the component holds an internal collection of 224 characters * 13 fonts * 4 font-weights = 11,648 entries describing each character width, for each font and each font-weight. Why not 255 characters? Because the first 31 characters are special control chars which are not relevant for this case.
So, considering the size of that internal collection, wherever you put the component it might really slow down the application where it is hosted. So don't put that component on a screen of the application that you are actually building and where you will want to have auto-width labels. Create a dedicated application to use this component.
You'll also find the component (with my other components) here on Github: e-gallis/PowerApps.
I hope you'll enjoy this little trick that just changed my life on Power Apps! 😉
If some of you wonder how I got the sizes for each character, font and font-weight... Well I used the native behavior of Power Apps that shifts the content of a label to the next line when it reaches its width's right border. Setting that label to AutoHeight, I made an application that would detect when the label's height would change. And so, for each character here is what I did:
Here's a animation of the app running... (funny to watch isn't it? 😁😞
You can download this FontCharsWidthBuilder app right below if you need to calculate the widths for other type of characters or fonts.
This component is also available on my Github Power Apps repo: https://github.com/e-gallis/PowerApps/tree/master/Components/AutoWidthLabelGenerator
Please don't forget to give a "thumbs up" to this post if you like it... 🙂
And do not hesitate to post your questions below...
Yours truly, @R3dKap
Really like the tool!! Great job!!
One thing I did notice was that you used a variable to store the table data. Trying to use that in a component doesn't seem to work for me so I changed it to a collection and now it works great. Not really sure why that should matter...just letting you know!
Well, in fact collections should not be used inside components as their scope is global and therefore they are available outside the component (which makes your collections in your application a bit messy: some are from the app, others come from components -> not very handy). That's why I used a variable instead.
About using the generated code inside a component, I would just pass the colCharsWidth variable to the component through a dedicated parameter and then adapt the formula that calulates the width of a label to use this parameter.
See what I mean?
Just updated the component so that, at step 3, the formula was changed to replace lblMyLabel with the Self operator. This means tha the formula will work immediately after pasting it to your labels Width property.
Great Component @R3dKap
This component came at perfect time for me. That saved my weekend trying to figure out how to solve this issue in our multilingual app
Thanks a lot for sharing. Appreciate your contribution to community !!
Thank you @prasaddjoshi, I'm glad it was usefull to you...
Would you be so kind (if that's possible) to put some captures of the screens where you used the component? I'd love to see what the community does with my components... 😊
Thanks. Glad you appreciate it...
Be aware though that I noticed that it doesn't work properly on mobile form factor applications. It looks like there is a form factor that is applied to fonts on mobile apps which prevents the label to display correctly. I still have to figure out a way to workaround this...
Usually, when this happens, it is due to the fact that you are using a font or a font-weight for your label that you didn't select in step 1 in the component. Meaning that your colCharsWidth collection in your OnStart event doesn't have the widths data for that font or that font-weight.
Can you check that and tell me here if it solves your problem?