以下のExtensionを使えば、TextViewの行数を取得できます。
Extension
extension UITextView { var numberOfLines: Int { var computingLineIndex = 0 var computingGlyphIndex = 0 while computingGlyphIndex < layoutManager.numberOfGlyphs { var lineRange = NSRange() layoutManager.lineFragmentRect(forGlyphAt: computingGlyphIndex, effectiveRange: &lineRange) computingGlyphIndex = NSMaxRange(lineRange) computingLineIndex += 1 } if textContainer.maximumNumberOfLines > 0 { return min(textContainer.maximumNumberOfLines, computingLineIndex) } else { return computingLineIndex } } }