4
我有一个UITextView
长字符串。文本视图大小由自动布局确定,并设置为截断文本。它看起来像这样:确定UITextView将截断字符串的位置
我怎样才能determing显示的字符串和被截断掉的范围的范围内?
我有一个UITextView
长字符串。文本视图大小由自动布局确定,并设置为截断文本。它看起来像这样:确定UITextView将截断字符串的位置
我怎样才能determing显示的字符串和被截断掉的范围的范围内?
试试这个
class ViewController: UIViewController ,NSLayoutManagerDelegate{
@IBOutlet weak var textView: UITextView!
var range :NSRange!
override func viewDidLoad() {
super.viewDidLoad()
textView.textContainer.layoutManager?.delegate = self
let str = textView.text as NSString
//Regardless of the repeated string
range = str.rangeOfString("cillium adipisicing")
print(range)
}
// Invoked while determining the soft line break point. When NO, NSLayoutManager tries to find the next line break opportunity before charIndex
func layoutManager(layoutManager: NSLayoutManager, shouldBreakLineByWordBeforeCharacterAtIndex charIndex: Int) -> Bool{
// charIndex is in the string or not
if range.location != NSNotFound && NSLocationInRange(charIndex, NSRange(location: range.location + 1, length: range.length - 1)) {
print("range of string is truncated -> charIndex is \(charIndex)")
}
return true
}
}
,但它仅限于BreakLineByWord
如果没有的话 “” 和截断,charIndex is 0
希望这会有所帮助:)