//文档
//显示是UIView的extension。UILabel, UITextField, UITextView都可以调用 extension UIView { ... open func sizeThatFits(_ size: CGSize) -> CGSize // return ‘best‘ size to fit given size. does not actually resize view. Default is return existing view size 获取最优size open func sizeToFit() // calls sizeThatFits: with current view bounds and changes bounds size. 调用sizeThatFits获取最优size,并修改为最优size } //使用 //sizeToFit textView.sizeToFit() //使用较多的是sizeThatFits,可用来在固定width时,根据text获取textView的高度 let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
原文:https://www.cnblogs.com/liuyongfa/p/9519141.html