uilabel - fit width to text

34 views (last 30 days)
Clemens Gersch
Clemens Gersch on 23 Jul 2020
Commented: Markus Leuthold on 25 Jul 2023
Hi,
I have some uilabel objectss in my appdesigner app which are created programmatically while the app is running.
Now I would like to change the size of the uilabel in a way that it fits the text and does not cut it off. How can I do that?
Googling gave me only some information on how to do that with uicontrol objects via the 'Extent' property. Unfortunately, uilabel objects do not have such a property and uicontrols don't work with appdesigner.
I would also be interested in how to fit the label size when the fontsize changes. Maybe that could be done using the same means.

Answers (2)

claudio duran
claudio duran on 13 Aug 2020
Hi! I have the same problem!
I use many uilabels in different windows and it is somehow not a good practice to set everything by hand.
It would be good to set the text to the length of the layout and, if the text is too large, instead of cutting it, it becomes automatically a new line. Do you know whether this is possible without the manual intervention?
Thanks in advance!

Harsha Priya Daggubati
Harsha Priya Daggubati on 27 Jul 2020
Hi,
Usually labels are supposed to be used for labelling purposes, which doesnot need any editing. So you can set the size manually by accessing the 'Position' property of uilabel, so that all the text fits into the label.
Label = uilabel(app.UIFigure);
Label.Text = 'Result';
size = Label.Position(3:4);
Label.Position(3:4) = [62 22];
Hope this helps!
  3 Comments
Walter Roberson
Walter Roberson on 13 Aug 2020
"Usually labels are supposed to be used for labelling purposes, which doesnot need any editing."
That is unrealistic, badly so.
  • font size changes to meet user vision needs
  • font size changes related to display resolution
  • automatic scaling is different in MATLAB for different operating systems running on the exact same hardware, 72 virtual ppi Mac, 100 virtual ppi Windows, no virtual ppi Linux, and the scaling only kicks in above a certain number of pixels for the display
  • high pixel count displays are not necessarily physically large displays, with there being a whole market of 1920 displays with different physical sizes, and 3840 displays can be 27", 31", 36", 51"...
  • it is completely unacceptable that the ux designer would have to hand-tweak the exact Position of every label or container object just because one spelling mistake was corrected or one copyright year was updated. Or, for that matter, that the user selected a different file in a display where the ux required that the complete name be used as a group label
  • It is unacceptable that the ux designer would have to hand tweak the Position of everything to support translation into a different user natural language
Any serious Ux needs the ability to query how much space an object would take up, so that design tools can automatically adjust component placement according to layout policies created by the ux developer.
... especially since the user should be able to grab the window and resize it, and the output components should reflow according to layout policies.
The ability to query required display size by program is required once you get passed the point of a program for internal use by a small number of people with the same hardware, and all of whom have good eyesight.
Markus Leuthold
Markus Leuthold on 25 Jul 2023
@Harsha Priya Daggubati, I take again your example and call
uilabel(Text="Result");
Error using uilabel
This functionality is not available on remote platforms.
So how would you recommend to set the width of uilabel without some hardcoded values? Are there any plans to improve this?

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!