创建一个 multi-line UILabel
Creating a multi-line UILabel
我正在尝试使用网站上的博客文章并将其显示在 iPhone 应用程序中。标题和副标题可以是multi-line.
我不知道如何根据内容更改标签的高度。
我可以使用 UITextView 实现此目的,但这是对 object 的正确使用吗?
谢谢!
~M
当然你可以根据文本动态改变UILabel的高度。只需确保 UILabel 的 numberOfLines = 0 并遵循此示例的答案:ios dynamic sizing labels
要获得完整答案,请使用以下代码...
计算标签中文本的高度,然后根据该大小设置标签的框架:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
我正在尝试使用网站上的博客文章并将其显示在 iPhone 应用程序中。标题和副标题可以是multi-line.
我不知道如何根据内容更改标签的高度。
我可以使用 UITextView 实现此目的,但这是对 object 的正确使用吗?
谢谢! ~M
当然你可以根据文本动态改变UILabel的高度。只需确保 UILabel 的 numberOfLines = 0 并遵循此示例的答案:ios dynamic sizing labels
要获得完整答案,请使用以下代码...
计算标签中文本的高度,然后根据该大小设置标签的框架:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;