How to adjust the height of a textview to his content in SWIFT?

前端 未结 7 744
无人及你
无人及你 2020-12-07 23:05

In my view controller, I have an UITextView. This textview is filled with a string. The string can be short or long. Depending on the length of the string, the height of the

7条回答
  •  Happy的楠姐
    2020-12-07 23:48

    In swift 2.3:

     func textViewDidChange(textView: UITextView) {
    
        let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.max))
        if size.height != TXV_Height.constant && size.height > textView.frame.size.height{
            TXV_Height.constant = size.height
            textView.setContentOffset(CGPointZero, animated: false)
        }
    }
    

提交回复
热议问题