我正在尝试获取特定范围(UITextRange)的rect。问题是代码在iOS 10上运行得很好,我得到了range和rect。不幸的是,对于iOS 9,这行代码:
if let rangeStart = textInput.position(from: textInput.beginningOfDocument, offset: location) 返回nil
let location = self.textView.firstRect(for: range)
extension NSRange {
func toTextRange(textInput:UITextInput) -> UITextRange? {
if let rangeStart = textInput.position(from: textInput.beginningOfDocument, offset: location),
let rangeEnd = textInput.position(from: rangeStart, offset: length) {
return textInput.textRange(from: rangeStart, to: rangeEnd)
}
return nil
}
}发布于 2017-01-14 12:48:38
textView.isSelectable = true
这一行在iOS 9中是必需的(在iOS 10中不是必需的)。
https://stackoverflow.com/questions/40462277
复制相似问题