Counting the number of lines in a UITextView, lines wrapped by frame size

前端 未结 13 1497
逝去的感伤
逝去的感伤 2020-11-27 03:42

I wanted to know when a text is wrapped by the frame of the text view is there any delimiter with which we can identify whether the text is wrapped or not.

For insta

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:03

    Swift extension:

    Using @himanshu padia answer

    //MARK: - UITextView
    extension UITextView{
    
        func numberOfLines() -> Int{
            if let fontUnwrapped = self.font{
                return Int(self.contentSize.height / fontUnwrapped.lineHeight)
            }
            return 0
        }
    
    }
    

    Usage : yourTextView.numberOfLines()

    be aware that if for some reason the font of the text view is nil, the return will be zero.

提交回复
热议问题