1

I have a reusable view class that I call when I want to add a disappearing subView to another view. I have a UILabel extension to determine when there is to much text for the label's current size(this extension works), and within this closure I'm trying to expand the: contianerView(regView)'s height, the label's height, and the label's height anchor, since the label was created programatically. As you guessed, the label isn't expandng correctly.

I've tried setting the numberOfLines to 0; changing the label's frame; using .layoutSubviews; removing when the height anchor was originally set, so now it's only called when the resize view method is used.

Label extension:

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
}
}

function to add reusable view(most of it is within the while loop towards the bottom):

    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
        })
    }

}

I've briefly done this before in storyboard where I had to expand a label within another view when the text was too long(this time it did work!), and the important part there was editing the height constraint, so I think this might have something to do with modifying the height constraint.

Any help would be greatly appreciated!

ANSWER:


I asked another question here: Programatically Created Label Within Container View Won't Expand For Text

it has the same code here and everything in the question but the answer works.

J Derbs
  • 153
  • 14

1 Answers1

0

If i understand correct, you have your view and a label, and you want your view to dynamically change height depend on label content. I suggest you to break that task to chunks and resolve it step by step.

1 - You might want to add a test UIView object instead of label with fixed size. When u do this, you will see whether you parent view expand depending of test view size.

2 - If it is, you are up to create a label with height you need. All that you need to know its font, text and width. I think this link may help you. After you sure, that your label size is correct (you may want to print it out) you may add it as any other UIView object to your parent view.

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • what do you mean with #1? I understand the concept, just not how to implement it. – J Derbs Jan 02 '19 at 03:56
  • @JackDerbis create an UIView programmatically. Set fixed width and height constraints, add colour, and add it to superview to see, if your logic is correct and view placed correctly and its visible. Then proceed with label. – Evgeniy Kleban Jan 02 '19 at 03:58
  • I still can't get it...I've tried the answers from so many questions but none of them work. – J Derbs Jan 02 '19 at 15:27
  • @JackDerbis what exactly you have not understand? – Evgeniy Kleban Jan 02 '19 at 16:49