swift: programmatically create UILabel fixed width that resizes vertically according to text length

后端 未结 2 916
攒了一身酷
攒了一身酷 2021-02-20 14:09

I\'ve seen answers to vertical resizing that involve autolayout, but the UILabels I\'m creating are only needed at runtime. (I might need anywhere from zero to many

2条回答
  •  既然无缘
    2021-02-20 14:45

    There are two usefull methods of UIView: sizeToFit() and sizeThatFits(_:)

    The first one resizes a view to a minimal size to fit subviews' content and the second one doesn't change frame at all, but returns calculated size which: (1) fit all subviews and (2) doesn't exceed parameter size

    So you can use sizeThatFits for you purpose:

    let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        label.backgroundColor = UIColor.orange
        label.textColor = UIColor.white
    //  label.text = "ultimate Frisbee"
        label.text = "ultimate Frisbee\nin 3 minutes,\nall welcome|2"
        label.numberOfLines = 10
        view.addSubview(label)
    
        updateLabelFrame()
    }
    
    func updateLabelFrame() {
        let maxSize = CGSize(width: 150, height: 300)
        let size = label.sizeThatFits(maxSize)
        label.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: size)
    }
    

    Output:

    P.S. You also can solve your problem with autolayout constraints, but I am not a big fan of using them programmatically.

提交回复
热议问题