【问题标题】:Creating a multi-line UILabel创建多行 UILabel
【发布时间】:2016-06-14 05:15:54
【问题描述】:

我正在尝试使用来自网站的博客文章并将其显示在 iPhone 应用程序中。标题和副标题可以是多行的。

我不知道如何根据内容更改标签的高度。

我可以使用 UITextView 来实现这一点,但这是对这个对象的正确使用吗?

谢谢! ~M

【问题讨论】:

  • 更多细节会更好。你用的是tableView吗?如果是这样,您是使用自定义 tableViewCell Nib,还是仅使用默认单元格?
  • 不...没有 tableView... 虽然我确实看到很多人都在使用这种方法,但也许我应该重新考虑使用简单的视图。谢谢!

标签: xcode swift uilabel


【解决方案1】:

当然,您可以根据文本动态更改 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多