我正在尝试使用网站上的博客文章并将它们显示在 iPhone 应用程序中。标题和副标题可以是多行的。
我不知道如何根据内容更改标签的高度。
我可以使用 UITextView 实现这一点,但这是对这个对象的正确使用吗?
谢谢! ~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;
关于xcode - 创建多行 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728813/