【问题标题】:Multiline UILabel inside UIScrollViewUIScrollView 内的多行 UILabel
【发布时间】:2014-12-31 20:01:25
【问题描述】:

我不能在滚动视图中为标签创建多行。尽管它在滚动视图之外完美地工作。详情见下图。滚动视图具有水平滚动。

UPD:滚动视图和标签约束:

【问题讨论】:

  • @rocky 好吧,没什么可显示的。基本上就是这样!

标签: ios uiscrollview uilabel


【解决方案1】:

显然我需要明确设置ScrollViewcontentSize。另外要获得这个尺寸,我需要从一个在这里有点棘手的标签中知道它。完整代码如下:

// create a label as usual
let label = UILabel()
label.numberOfLines = 0
label.text = "really long label text..."

// place it with a nice offset and set its width explicitly using parent view width
// after which `sizeToFit` does a trick and adjusts it's height
label.frame.offset(dx: 16.0, dy: 16.0)
label.frame.size.width = self.view.frame.width - 32
label.sizeToFit()

// now add it to the scroll view and set content size to label size plus margin at the bottom
self.scrollView.addSubview(label)
self.scrollView.contentSize = label.frame.size
self.scrollView.contentSize.height += 32

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多