确定 UITextView 将截断字符串的位置

Wii*_*ard 5 string uitextview ios autolayout swift

我有一个UITextView很长的字符串。文本视图大小由自动布局决定,并设置为尾截断文本。它看起来像这样:

UITextView 截断

如何确定显示的字符串范围和被截断的范围?

Wil*_* XJ 1

尝试这个

\n\n
class ViewController: UIViewController ,NSLayoutManagerDelegate{\n\n@IBOutlet weak var textView: UITextView!\nvar range :NSRange!\noverride func viewDidLoad() {\n    super.viewDidLoad()\n\n\n    textView.textContainer.layoutManager?.delegate = self\n    let str = textView.text as NSString\n    //Regardless of the repeated string\n    range = str.rangeOfString("cillium adipisicing")\n    print(range)\n\n\n}\n\n// Invoked while determining the soft line break point.  When NO, NSLayoutManager tries to find the next line break opportunity before charIndex\nfunc layoutManager(layoutManager: NSLayoutManager, shouldBreakLineByWordBeforeCharacterAtIndex charIndex: Int) -> Bool{\n\n    // charIndex is in the string or not\n    if range.location != NSNotFound && NSLocationInRange(charIndex, NSRange(location: range.location + 1, length: range.length - 1 )) {\n        print("range of string is truncated -> charIndex is \\(charIndex)")\n    }\n    return  true\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但仅限于 BreakLineByWord没有“”且被截断的 if 单词 \xef\xbc\x8ccharIndex is 0

\n\n

希望它有帮助:)

\n