速译:
guard let font1: UIFont = textView.font else { return }
var lines: [String] = []
let maxWidth: CGFloat = textView.frame.width
let s: NSAttributedString = NSAttributedString.init(string: textView.text, attributes: [.font: font1])
let tc: NSTextContainer = NSTextContainer.init(size: CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude))
let lm: NSLayoutManager = NSLayoutManager.init()
let tm: NSTextStorage = NSTextStorage.init(attributedString: s)
tm.addLayoutManager(lm)
lm.addTextContainer(tc)
lm.enumerateLineFragments(forGlyphRange: NSRange(location: 0, length: lm.numberOfGlyphs)) { (rect: CGRect, usedRect: CGRect, textContainer: NSTextContainer, glyphRange: NSRange, Bool) in
let r: NSRange = lm.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
let str = s as NSAttributedString
let s2 = str.attributedSubstring(from: r)
print(s2)
lines.append(s2.string)
}