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

lf215 picture lf215 · Nov 12, 2016 · Viewed 22.7k times · Source

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 of these labels.)

Examples (ignore the color)

  1. Short text (note same width as longer text):

enter image description here

  1. Longer text (note same width as shorter text example with more lines for add'l text):

enter image description here

If the text can fit in one line of fixed width, the label shouldn't need to resize vertically. But if there are more characters, the label should keep expanding vertically to fit these additional characters. The text should keep wrapping around line after line. The text should start in the top left corner of the label.

To be more specific:

let marker = GMSMarker(position: myLatLng)
// see http://stackoverflow.com/a/40211383/1168364 for imageWithView
marker.icon = imageWithView(label) // **how do i create this label?**
marker.map = map // map is a GMSMapView

These labels can be anywhere on the screen. This is for a map application where each label will be placed at a random location. The labels' locations have no relationship to one another.

Answer

alexburtnik picture alexburtnik · Nov 13, 2016

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:

enter image description here enter image description here

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