代码改变世界

sizeToFit 和 sizeThatFits

2018-08-22 17:32  法子  阅读(493)  评论(0编辑  收藏  举报
//文档
//显示是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))