2

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?

user1007522
  • 7,858
  • 17
  • 69
  • 113

5 Answers5

3

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

Manobala
  • 274
  • 2
  • 7
1

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.

JingJingTao
  • 1,760
  • 17
  • 28
  • but I have that constraint on the left side. It must be 20 or bigger so that it can shrink when the textlabel is short but now the text label is big so it can grow until it comes to that constraint 20 px offset left. – user1007522 Sep 07 '16 at 12:04
  • okay so currently the left constraint is set to something like greater than or equal to 20? Could you tell me what the leading and trailing constraints are for the label? – JingJingTao Sep 07 '16 at 12:07
  • I don't have any leading and trailing. I'm using the left and right. Is that not the same? Doesn't it only matter when you are support foreign languages? – user1007522 Sep 07 '16 at 12:09
  • yes the left and right constraint, could you tell me what then are currently set to, I've use to saying leading and trailing :p – JingJingTao Sep 07 '16 at 12:10
  • No problem :-), yes this are the constraints currently: self.container.autoPinEdge(.Right, toEdge: .Left, ofView: self.rightView, withOffset: -20, relation: .LessThanOrEqual) self.container.autoPinEdgeToSuperviewEdge(.Left, withInset: 20, relation: .GreaterThanOrEqual) – user1007522 Sep 07 '16 at 12:11
  • sorry I see it in your code, so your container is offset by 20 and your label is offset by 10, maybe change your label's left edge the same as your container's left edge? Actually do you need the container, you want rounded edges, is that why you are using it? – JingJingTao Sep 07 '16 at 12:18
  • The container has a black color like on the screenshot. The problem is that the label is not growing for some reason... – user1007522 Sep 07 '16 at 12:19
  • Could we continue this in chat? It looks like your container is probably what we need to change it's constraints. http://chat.stackoverflow.com/rooms/26424/iosandroidchaosoverflow – JingJingTao Sep 07 '16 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122825/discussion-between-user1007522-and-jingjingtao). – user1007522 Sep 07 '16 at 12:26
0

Try this

testLabel.text = "long text......."
testLabel.numberOfLines = 0
testLabel.sizeToFit()
Jeyamahesan
  • 1,101
  • 7
  • 15
0

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

0

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];
Saranjith
  • 11,242
  • 5
  • 69
  • 122