简体   繁体   English

Swift 3 UILabel获取实际高度(intrinsicContentSize)

[英]Swift 3 UILabel get actual height (intrinsicContentSize)

I'm trying to simulate a kind of "Please wait..." UILabel. 我正试图模拟一种“请等待......”UILabel。 The label's text must be regularly updated. 标签的文本必须定期更新。 So far everything works as expected. 到目前为止一切都按预期工作。 However, I need to get the intrinsic content height of the label to be able to position its container view (UIView). 但是,我需要获得标签的内在内容高度才能定位其容器视图(UIView)。

在此输入图像描述

The label is the one with the red background, whereas the one with the white background is its container. 标签是具有红色背景的标签,而具有白色背景的标签是其容器。

I've tried a few different approaches, unfortunately, all in vain. 不幸的是,我尝试了几种不同的方法,都是徒劳的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

private func createBusyLabel(labelText: String) -> CGFloat {

    self.busyViewContainer.addSubview(self.busyLabel)

    self.busyLabel.backgroundColor = UIColor.red

    self.busyLabel.numberOfLines = 0
    self.busyLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    self.busyLabel.sizeToFit()

    //set the constraints, but skip height constraints
    self.busyLabel.translatesAutoresizingMaskIntoConstraints = false
    self.busyLabel.horizontalLeft(toItem: self.busyViewContainer, constant: 60)
    self.busyLabel.horizontalRight(toItem: self.busyViewContainer, constant: -10)
    self.busyLabel.topConstraints(toItem: self.busyViewContainer, constant: 10)


    self.busyLabel.text = labelText
    //calculate height with margin
    let height: CGFloat = self.busyLabel.intrinsicContentSize.height + 20
    return height

}

Also, the line counting function, from a previously asked and already answered question , delivers only 1 此外,行计数功能,从先前提出的和已经回答的问题 ,仅提供1

Here is what it look like after I set the bottom constraint: 这是我设置底部约束后的样子:

在此输入图像描述

The simple fix is adding the bottom constraint between 'self.busyViewContainer' and its superview. 简单的解决方法是在'self.busyViewContainer'和它的superview之间添加底部约束。

Following your code and syntax it can be something like this: 遵循您的代码和语法,它可以是这样的:

self.busyLabel.bottomConstraints(toItem: self.busyViewContainer, constant: 10)

It is a common problem with 'Unsatisfiable Constraints'. 这是“不满意的约束”的常见问题。 The autolayout should ensure it satisfies horizontal axis layout so it needs to have both top and bottom constraints in this case. 自动布局应确保其满足水平轴布局,因此在这种情况下需要同时具有顶部和底部约束。

Apple doc - Unsatisfiable Constraints Apple doc - 不满意的约束

Apple doc - Logical Errors Apple doc - 逻辑错误

UPD: In this case, the layout of the superview can define a height with intrinsicContentSize : UPD:在这种情况下,superview的布局可以使用intrinsicContentSize定义高度:

var intrinsicContentSize: CGSize { return ... }

Where the height of the parent view will be calculated based on the label one. 其中父视图的高度将根据标签1计算。

A million Thanks to ozgur, who changed my approach. 百万感谢ozgur,他改变了我的方法。 Ozgur, your code works perfect, but unfortunately not for me, as I faced problems with bottomLayoutGuide part. Ozgur,你的代码很完美,但遗憾的是不适合我,因为我遇到了bottomLayoutGuide部分的问题。 The reason for this is that the label and its container are created in an external class. 原因是标签及其容器是在外部类中创建的。

Previously I tried to set bottom constraint to the label, which did not return the expected result. 以前我试图为标签设置底部约束,但没有返回预期的结果。 However, inspired by ozgur's answer, this time I simply set the bottom constraint to its container and not the label, giving in expected result, like following: 然而,受到ozgur答案的启发,这次我只是将底部约束设置为其容器而不是标签,给出预期结果,如下所示:

    self.busyViewContainer.bottomConstraints(toItem: self.busyLabel, constant: 10)

Thanks to all who put in their precious efforts. 感谢所有付出宝贵努力的人。

private func createBusyLabel(labelText: String) -> Void {

    self.busyLabel.text = labelText
    self.busyLabel.font = UIFont.getGlobalFont(size: _textSizeSmall, type: "bold")

    self.busyLabel.backgroundColor = UIColor.red

    // handle multiline problem
    self.busyLabel.numberOfLines = 0
    self.busyLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    self.busyLabel.sizeToFit()

    self.busyViewContainer.addSubview(self.busyLabel)
    self.busyLabel.translatesAutoresizingMaskIntoConstraints = false
    self.busyLabel.horizontalLeft(toItem: self.busyViewContainer, constant: 60)
    self.busyLabel.horizontalRight(toItem: self.busyViewContainer, constant: -10)
    self.busyLabel.topConstraints(toItem: self.busyViewContainer, constant: 10)

    // the following line made the difference
    self.busyViewContainer.bottomConstraints(toItem: self.busyLabel, constant: 10)

}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM