PoolHallJunkie
PoolHallJunkie

Reputation: 1363

UITextInput returns nil position only in iOS 9

I am trying to get the rect of a specific range (UITextRange). The problem is that the code works fine with iOS 10 , and I am getting the range and rect. Unfortunately with iOS 9 the line :

if let rangeStart = textInput.position(from: textInput.beginningOfDocument, offset: location) 

returns 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
    }
}

Upvotes: 3

Views: 349

Answers (1)

hengyu
hengyu

Reputation: 76

textView.isSelectable = true

This line is necessary in iOS 9 (not necessary in iOS 10).

Upvotes: 1

Related Questions