【问题标题】:UITextInput returns nil position only in iOS 9UITextInput 仅在 iOS 9 中返回零位置
【发布时间】:2017-03-20 15:00:21
【问题描述】:

我正在尝试获取特定范围的矩形 (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
    }
}

【问题讨论】:

    标签: ios swift uitextview uitextinput uitextrange


    【解决方案1】:

    textView.isSelectable = true

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

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2015-08-11
      • 2016-03-03
      • 2017-03-12
      • 2015-09-01
      • 2016-01-25
      • 2017-07-26
      • 1970-01-01
      相关资源
      最近更新 更多