Swift: programmatically creates a fixed UILabel width that changes vertically according to the length of the text

I saw answers to vertical resizing that includes autostart, but the UILabel that I create is only needed at runtime. (I may need from zero to many of these labels.)

Examples (ignore color)

  • Short text (note the same width as the longer text):

enter image description here

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

enter image description here

If the text can fit on one line of fixed width, the label does not need to be resized vertically. But if there are more characters, the label must expand vertically to match these additional characters. The text should contain the stroke of the line after the line. The text should begin in the upper left corner of the label.

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 shortcuts can be anywhere on the screen. This is for a map application where each label will be placed in a random place. The location of the labels is not related to each other.

+4
ios uilabel swift autoresize
source share
2 answers

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

The first resizes the minimum size to fit the content of the subviews, and the second does not change the frame at all, but returns a calculated size that: (1) matches all subzones and (2) does not exceed the size parameter

So you can use sizeThatFits :

 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

PS You can also solve your problem with auto-detection restrictions, but I'm not a big fan of using them programmatically.

+7
source share

Set the label numberOfLines property to zero and fix the width of your labels with restrictions (either by limiting the width explicitly, or by restricting the leading and trailing edges to another representation, for example, the label label). Then the labels will automatically change vertically to the height corresponding to the text. You will need to connect one label using its upper limit, so that the presentation system knows where to start the layout, then the top of all other labels should be limited to the bottom of the previous label. Thus, they will correspond to the layout relative to the height of the previous label.

in response to your comment:

You can set the width of the view explicitly on iOS 9 and later using the widthAnchor property. You can install it on older versions of iOS using NSLayoutConstraints (SO search for examples).

eg:.

label.widthAnchor.constraintEqualToConstant(50.0).active = true

This would set the width of the label to a width of 50 points, but not fix its height, so using numberOfLines = 0 label will automatically resize vertically when you set the text.

+1
source share

All Articles