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

前端 未结 13 1476
逝去的感伤
逝去的感伤 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:13

    Swift 5.0

    extension UITextView {
        func sizeFit(width: CGFloat) -> CGSize {
            let fixedWidth = width
            let newSize = sizeThatFits(CGSize(width: fixedWidth, height: .greatestFiniteMagnitude))
            return CGSize(width: fixedWidth, height: newSize.height)
        }
    
        func numberOfLine() -> Int {
            let size = self.sizeFit(width: self.bounds.width)
            let numLines = Int(size.height / (self.font?.lineHeight ?? 1.0))
            return numLines
        }
    }
    

提交回复
热议问题