3

我正在尝试获取特定范围的矩形(UITextRange)。问题是代码可以正常工作iOS 10,并且我得到了范围和矩形。不幸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
    }
}
4

1 回答 1

1

textView.isSelectable = true

此行在 iOS 9 中是必需的(在 iOS 10 中不是必需的)。

于 2017-01-14T04:48:38.220 回答