调整标签大小以适应文本数量- Swift

时间:2023-01-27 17:29:05

Hey :) I have a label and I need to make the width of that label smaller or larger accourding to the text amount, and I found only how to adjust the text to fit the size but the how to adjust the size to fit the text, any ideas ?

嘿:)我有一个标签,我需要做标签的宽度小的或大的急行军的文本数量,我只找到如何调整文本大小适合,但如何调整大小以适应文本,任何想法?

1 个解决方案

#1


27  

You'll want to do this:

你会想这样做:

myLabel.sizeToFit()

As seen here: https://developer.apple.com/documentation/uikit/uiview/1622630-sizetofit

如图所示:https://developer.apple.com/documentation/uikit/uiview/1622630-sizetofit

This will update the label's frame to fit the content. You can then place it or make any edits after this that you want.

这将更新标签的框架以适应内容。然后,您可以放置它或在此之后进行任何您想要的编辑。

#1


27  

You'll want to do this:

你会想这样做:

myLabel.sizeToFit()

As seen here: https://developer.apple.com/documentation/uikit/uiview/1622630-sizetofit

如图所示:https://developer.apple.com/documentation/uikit/uiview/1622630-sizetofit

This will update the label's frame to fit the content. You can then place it or make any edits after this that you want.

这将更新标签的框架以适应内容。然后,您可以放置它或在此之后进行任何您想要的编辑。