我有一个多行的UILabel:
let serviceDescription: UILabel = UILabel() serviceDescription.text =
"some very long text..."
serviceDescription.numberOfLines = 0
serviceDescription.sizeToFit()
self.contentView.addSubview(serviceDescription)然后我添加了一些自动收费约束:
serviceDescription.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.contentView_snp_top).offset(24)
make.left.equalTo(self.contentView).offset(20)
make.right.equalTo(self.contentView).offset(-20)
}现在,我想计算这个标签在屏幕上需要的大小,所以我做了:
let newSize: CGSize = serviceDescription.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
println("serviceDescription size: \(newSize)")结果是:
(3408.0, 95.5)我在viewDidLoad()中执行了前面的所有命令。
如何获得屏幕上标签的正确大小?
发布于 2015-05-31 16:09:31
你不需要计算它的高度,标签在放置好后就知道它的高度,
override func viewDidLoad() {
super.viewDidLoad()
println(label.frame.height) // 21
label.text = "Some long text to make the text go over more than one line"
label.layoutIfNeeded()
println(label.frame.height) // 101.5
}https://stackoverflow.com/questions/30559157
复制相似问题