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

橙三吉。 提交于 2019-12-21 09:33:57

问题


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

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

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.


回答1:


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.




回答2:


Set the label's numberOfLines property to zero and fix the width of your labels with constraints (either by constraining the width explicitly or by constraining the leading and trailing edges to some other view like the label's superview). Then the labels will resize vertically automatically to the height that fits the text. You'll need to pin one label via its top constraint so the view system knows where to start the layout, then the top of all your other labels should be constrained to the bottom of the previous label. This way they will all layout relative to the height of the previous label.

edit in response to your comment:

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

e.g.:

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

This would set the label's width to 50 points wide, but not fix its height, so with numberOfLines = 0 then the label will auto-resize vertically when you set the text.



来源:https://stackoverflow.com/questions/40567804/swift-programmatically-create-uilabel-fixed-width-that-resizes-vertically-accor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!