MATLAB Answers

uilabel - fit width to text

23 views (last 30 days)
Clemens Gersch
Clemens Gersch on 23 Jul 2020
Commented: Walter Roberson on 13 Aug 2020
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.

  0 Comments

Sign in to comment.

Answers (2)

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!

  2 Comments

Clemens Gersch
Clemens Gersch on 27 Jul 2020
Thanks for the effort. I know about that functionality and that is what I am doing right now.
However, I would like to make my app easier to modify. For example, I have a constant in my code that I can change so that the font size of all components becomes bigger. Right now, I cannot properly use ist, as I would also have to find the optimal size of each label depending on the font size manually. As I have a lot of labels, this is not possible to do.
I also would like to set spaces between my components exactly. If I try to fit the size of the label manually, I am not able to do so, because I set the boarders of the the label with error. Of course, if I set the text of my label right-adjusted, it is no problem to place a label next to a field which is right to it. But I usually have to align the text somehow to the left and the right (for example, if the lable is placed between two other components) and therefore I need a possibility to measure text length or fit the lable directly.
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.

Sign in to comment.


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!

  0 Comments

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by