label is not growing when the text is longer

2k Views Asked by At

I want a view that is centered in the superview but that grows due the content in this case a label. But I don't want it to grow that it doesn't fit in the screen anymore so thats why I pin the left and right.

I've put on a test viewcontroller:

import UIKit
import PureLayout

final class ViewController: UIViewController {

    let container: UIView = {
        let container = UIView(forAutoLayout: ())
        container.backgroundColor = UIColor.blackColor()
        container.clipsToBounds = true
        return container
    }()

    let label: UILabel = {
        let label = UILabel(forAutoLayout: ())
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 1
        label.textColor = UIColor.redColor()
        label.text = "This is a very very very long message"
        return label
    }()

    var rightView: UIView = {
        let view = UIView(forAutoLayout: ())
        view.backgroundColor = .redColor()

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.container.addSubview(self.label)
        self.view.addSubview(self.container)
        self.view.addSubview(self.rightView)

        self.container.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 20)
        self.container.autoAlignAxisToSuperviewAxis(.Vertical)
        self.container.autoSetDimension(.Height, toSize: 36)
        self.container.layer.cornerRadius = 18
        self.container.autoPinEdge(.Right, toEdge: .Left, ofView: self.rightView, withOffset: -20, relation: .LessThanOrEqual)
        self.container.autoPinEdgeToSuperviewEdge(.Left, withInset: 20, relation: .GreaterThanOrEqual)
        self.container.setContentCompressionResistancePriority(UILayoutPriorityRequired, forAxis: .Horizontal)

        self.label.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsMake(10, 20, 10, 20))

        self.rightView.autoPinEdgeToSuperviewEdge(.Right, withInset: 5)
        self.rightView.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 20)
        self.rightView.autoSetDimension(.Width, toSize: 50)
        self.rightView.autoSetDimension(.Height, toSize: 60)
    }
}

The result of this is:

enter image description here

Why is the black view not growing until it can't due the left and right constraint? The number of lines is 1 and the ContentCompressionResistancePriority is on?

5

There are 5 best solutions below

1
Jeyamahesan On

Try this

testLabel.text = "long text......."
testLabel.numberOfLines = 0
testLabel.sizeToFit()
2
Muruganandam Sathasivam On

Set the label.numberOfLines = 0 in your code then only it will expand based the text that you have.

9
JingJingTao On

What are the leading and trailing constraints on the label, you could set the leading constraint to an inequality like greater than or equal something like 5. If you're happy for the font to get smaller, you could set adjustsFontSizeToFitWidth to YES.

3
Manobala On

You can change the font size to fit the width of the UILabel (non-multiline):

label.numberOfLines = 1; label.adjustsFontSizeToFitWidth = true; label.sizeToFit(); You need dynamic changes in height means then you can do like following

Dynamic UILabel changes

label.preferredMaxLayoutWidth = 500;

you can use the above code for set preferred max width, All the best :)

0
Saranjith On

Im using xcode 4. I have tested this issue

Xcode screenshot

enter image description here

After running in iPhone 6

enter image description here Since you have a view besides the label,It wont come delete or move the view to another layer so that the label resizes automatically with code

[labelName sizeToFit];