Swift UILabel height adaptive (when the UILabel height and width dynamically adapt to the number of words in the text, the upper and lower parts of the UILabel will be left blank and cannot be filled up)

When using UILabel in a project, you usually need to implement height adaptation. The method is to first calculate the number of text fonts and then calculate the required height:

Methods as below:

let label = UILabel()
label.backgroundColor = UIColor.gray
label.text = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest"
        
label.font = UIFont.systemFont(ofSize: 15)
label.textColor = UIColor.red
label.textAlignment = .left
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
let boundingRect = label.text!.boundingRect(with: CGSize(width: 200, height: 0), options: .usesLineFragmentOrigin, attributes:[NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14)], context: nil)
label.frame = CGRect(x: 10, y: 200, width: self.view.frame.width - 20, height: boundingRect.height)
self.view.addSubview(label)

The effect is as follows:

We will find that there are gaps in the upper and lower parts of the label that we cannot fill. This will cause the layout of the UI to be messed up if we need to add UI below and above our label.

Then the solution is very simple. You only need to change the method of calculating the number of text words and set the height to CGFloat (MAXFLOAT) . The method is as follows:

let label = UILabel()
label.backgroundColor = UIColor.gray
label.text = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest"
        
label.font = UIFont.systemFont(ofSize: 15)
label.textColor = UIColor.red
label.textAlignment = .left
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
let size = label.sizeThatFits(CGSize(width: self.view.frame.width, height: CGFloat(MAXFLOAT)))
label.frame = CGRect(x: 10, y: 200, width: self.view.frame.width - 20, height: size.height)
self.view.addSubview(label)

The result is as follows:

Guess you like

Origin blog.csdn.net/qq_37269542/article/details/89467969