2016-11-07 1 views
3

특정 범위 (UITextRange)의 rect를 얻으려고합니다. 문제는 코드가 iOS 10과 함께 작동하며 범위와 rect가 입니다. 불행하게도 라인 iOS 9과 :UITextInput은 iOS 9에서만 nil 위치를 반환합니다.

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

반환

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

답변

0

textView.isSelectable = true

이 줄은 (아이폰 OS 10에서 필요 없음) 아이폰 OS 9에서 필요 nil.