03-28-2022 11:27 AM
Note (12-1-2022): this technique is a workaround solution to the absence of native auto-width labels in Power Apps. Now I recommend using the CreatorKit made by the Microsoft PowerCAT team which provides fluent UI controls among which an auto-width label (but much much more... resizable text areas, breadcrumbs, command bars, expand/context/navigation menus, progress bars & spinners, pickers, panels, dialogs, ...).
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
Sorry for the delay. Were you able to fix this issue or is it still stuck at size 10? Can you tell me for which font and font-weight?
Hi. Really like your component and have used it before. Not sure if you have updated this elsewhere but if you "Cut and paste" the same code and button from your Standard Power Apps application then restart the Teams app, it works nice.
Thanks for sharing your awesome component.
I'm trying to find this app now that I'm using it and I can't seem to find it in PowerApps. Does it get put in a different section than "Apps"?
It's not an app, it's a component. You need to create a new Power Apps canvas app and import the component (the attached MSAPP file here on the original post) and then add it to a blank screen to use it to generate the code that you will put on your labels in your own apps... Am I clear enough?
You have made a really great component, love it!
I have built a Canvas App, but as a Custom Page for my Model Driven App.
Thought I'd share this in case someone wants to build a custom page and use this.
So I was able to discover that nor Self nor the control itself has access to Font, FontWeight, Size, PaddingLeft and PaddingRight.
The only way you can get it to work is by using a component inside a Custom Page, as it does have access to those properties.
I do have to say that it might appear to be buggy in the start, as it seems like the label has a width of 10px.
In case that happens saving, publishing, refreshing the page and launching the model driven app and everything should look completely normal, same for when you go edit the custom page again.
Hope this helps!
PS: This might change soon since custom pages is quite the new feature so
Very interesting feedback @Billy_C...
When you say the only way to make it work on a model-driven custom page is through a component, does it mean that any label on which you want auto-width with the formula provided by my component must be placed itself inside a component?
Yes, any label you want to use the auto width on has to be inside a component, it's not possible to do this directly within the model driven custom page due to the lack of access to the used properties.
Also, when you develop the component you want to set the default value of the table input variable to a minimal auto width formula, otherwise you won't be able to properly view the labels since it will have a width of 10px. This can be reduced to a single row safely as usual once development of the component is finished to minimize the size of the component, but this is of course completely optional.