ios - UITextInput 仅在 iOS 9 中返回 nil 位置

标签 ios swift uitextview uitextinput uitextrange

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

最佳答案

textView.isSelectable = true

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

关于ios - UITextInput 仅在 iOS 9 中返回 nil 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462277/

相关文章:

php - Swift3:参数除以 "/"的HTTP POST请求

ios - 如何在 Swift 中访问存储在字典中的数组?

ios - 在 IOS Swift 中定期向用户发送通知

ios - iOS-自定义键盘-在不松开手指的情况下使用重音键

iphone - UItextView 检测关闭高亮链接、电话号码等

ios - 在设备方向上设置 UITextVIew 位置

iOS 在什么情况下是 Controller :didChangeSection:atIndex:forChangeType: called

iphone - iOS Box 应用程序是否响应自定义 URL 方案?

ios - 尽管启用了透明度,但半透明的导航和标签栏看起来不透明

ios - 如果发生溢出,如何为 UITextView 设置最大高度和固定宽度并强制省略而不是滚动?