xcode - 创建多行 UILabel

标签 xcode swift uilabel

我正在尝试使用网站上的博客文章并将它们显示在 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/

相关文章:

ios - 通过 CAGradientLayer 在 IOS 的 UIImageView 中添加渐变层

iphone - UILabel 左右对齐

ios - 初始化后在 UITextField 的子类上设置 rightview

ios - UIlabel 问题 : Keep the string value after app is closed?

ios - 如何在Swift中构建React Native包?

xcode - 如何在 os-signpost-point-schema 中指定符号/字符

swift - 后台线程中的 moya 请求

ios - 如何定位我的 footerView 使其位于 UITableView 的底部?

ios - 在第四代 iPad 上使用 Eureka Pod

ios - 代码不再在 OS4.3 中工作但在 5.0 中正常(过去工作正常)