uilabel - fit width to text

13 ビュー (過去 30 日間)
Clemens Gersch
Clemens Gersch 2020 年 7 月 23 日
編集済み: dpb 2024 年 7 月 31 日
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.

回答 (2 件)

claudio duran
claudio duran 2020 年 8 月 13 日
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 2020 年 7 月 27 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 13 日
編集済み: dpb 2024 年 7 月 31 日
"Usually labels are supposed to be used for labelling purposes, which does not 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 past 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 2023 年 7 月 25 日
@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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by