UILabel is not auto-shrinking text to fit label size

后端 未结 17 643
感动是毒
感动是毒 2020-12-04 06:53

I have this strange issue, and im dealing with it for more than 8 hours now.. Depending on situation i have to calculate UILabels size dynamically,
e.g

17条回答
  •  臣服心动
    2020-12-04 07:25

    In Swift 4 (Programmatically):

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200.0, height: 200.0))
    label.adjustsFontSizeToFitWidth = true
    label.numberOfLines = 0
    
    label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    
    view.addSubview(label)
    

提交回复
热议问题