尽管有几种方法,标签仍无法在容器内调整大小

时间:2019-01-01 00:19:31

标签: swift resize uilabel

我想在其他视图中添加消失的subView时调用一个可重用的视图类。我有一个UILabel扩展名,以确定何时有很多文本适合标签的当前大小(此扩展名有效),在此闭合中,我尝试扩展:contianerView(regView)'s height,即标签的高度,以及标签的高度锚点,因为标签是通过编程方式创建的。您猜到了,标签没有正确展开。

我尝试将numberOfLines设置为0;更改标签的框架;使用.layoutSubviews;最初设置高度锚点时将其删除,因此现在仅在使用调整大小视图方法时调用它。

标签扩展名:

extension UILabel {

var isTruncated: Bool {

    guard let labelText = text else {
        return false
    }

    let labelTextSize = (labelText as NSString).boundingRect(
        with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude),
        options: .usesLineFragmentOrigin,
        attributes: [.font: font],
        context: nil).size

    return labelTextSize.height > bounds.size.height
}
}

用于添加可重用视图的函数(大多数视图位于while循环的底部):

    func addDisapearingView(toview: UIView, text: String, textColor: UIColor, colorView: UIColor, alpha: CGFloat, height: CGFloat){

    regView.backgroundColor = colorView
    regView.alpha = alpha
    toview.addSubview(regView)

    regView.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
        let guide = toview.safeAreaLayoutGuide
        regView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        regView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        regView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        UIView.animate(withDuration: 5.0) {
            self.regView.heightAnchor.constraint(equalToConstant: height).isActive = true
        }

    } else {
        NSLayoutConstraint(item: regView,
                           attribute: .top,
                           relatedBy: .equal,
                           toItem: toview, attribute: .top,
                           multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: regView,
                           attribute: .leading,
                           relatedBy: .equal, toItem: toview,
                           attribute: .leading,
                           multiplier: 1.0,
                           constant: 0).isActive = true
        NSLayoutConstraint(item: regView, attribute: .trailing,
                           relatedBy: .equal,
                           toItem: toview,
                           attribute: .trailing,
                           multiplier: 1.0,
                           constant: 0).isActive = true

        regView.heightAnchor.constraint(equalToConstant: height).isActive = true
    }

    let label = UILabel(frame: CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.bounds.width, height: regView.bounds.height))
    label.numberOfLines = 0
    label.adjustsFontSizeToFitWidth = true
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.translatesAutoresizingMaskIntoConstraints = false
    label.center.x = newView.center.x
    label.center.y = newView.center.y
    label.textAlignment = .center
    label.text = text
    label.textColor = textColor
    regView.addSubview(label)
    if label.isTruncated {
        print("LABEL IS TRUNCATED")
    }
    //test if there is more text than the label has room for
    while label.isTruncated {
        print("printing while truncating in the wHiLE loop")
        regView.bounds.size.height += 5
        label.bounds.size.height += 5
        var currentLabelHeight = label.bounds.height
        let amt = currentLabelHeight + 5
        label.frame = CGRect(x: regView.frame.origin.x, y: regView.frame.origin.y, width: regView.bounds.width, height: CGFloat(amt))
        var heighT : CGFloat = height
        heighT += 5
        regView.heightAnchor.constraint(equalToConstant: heighT).isActive = true

    }
    regView.layoutSubviews()
    label.sizeToFit()
    //remove
    Timer.scheduledTimer(withTimeInterval: 2.8, repeats: false) { (action) in
        UIView.animate(withDuration: 2.8, animations: {
            self.regView.heightAnchor.constraint(equalToConstant: 0).isActive = true
            label.heightAnchor.constraint(equalToConstant: 0).isActive = true
        })
    }

}

我之前在storyboard中曾做过简短的操作,在此过程中,当文本太长(这次确实起作用了!)时,我不得不在另一个视图中展开标签,其中重要的部分是编辑高度约束,所以我认为这可能与修改高度约束有关。

任何帮助将不胜感激!

答案:


我在这里问了另一个问题:Programatically Created Label Within Container View Won't Expand For Text

这里的代码和问题中的所有内容都相同,但答案有效。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,则您拥有视图和标签,并且希望视图根据标签内容动态更改高度。我建议您将任务分解为多个部分并逐步解决。

1-您可能想添加测试UIView对象而不是固定大小的标签。当您执行此操作时,您将看到您的父视图是否根据测试视图的大小展开。

2-如果是,则可以创建具有所需高度的标签。所有您需要知道的字体,文本和宽度。我认为this链接可能会对您有所帮助。确定您的标签大小正确之后(您可能希望将其打印出来),可以将其作为其他UIView对象添加到父视图中。