OGeek|极客世界-中国程序员成长平台

标题: ios - 第一个矩形(对于 :) returns wrong value in UITextField [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:34
标题: ios - 第一个矩形(对于 :) returns wrong value in UITextField

我正在尝试使用语法高亮来标记用户输入的错误。

遵循 Get X and Y coordinates of a word in UITextView 中的建议我的代码非常适合 UITextView

但是,我现在正在尝试为 UITextField 做类似的事情并且遇到了麻烦。

UITextField 和 UITextView 都符合 UITextInput 并且定位矩形所需的所有方法都适用于两者。

这是 UITextView 的函数

func findRect(forTextMatching match:String, in textView:UITextView) -> CGRect? {
    if let text = textView.text,
       let range = text.range(of: match)
    {
        let offset1 = text.distance(from: text.startIndex, to: range.lowerBound)
        let offset2 = text.distance(from: text.startIndex, to: range.upperBound)

        if let pos1 = textView.position(from: textView.beginningOfDocument, offset: offset1),
        let pos2 = textView.position(from: textView.beginningOfDocument, offset: offset2)
        {
            if let textRange = textView.textRange(from: pos1, to: pos2)
            {
                textView.selectedTextRange = textRange
                let rect = textView.firstRect(for: textRange)
                return rect
            }
        }
    }

    return nil
}

在测试应用程序中运行它并突出显示返回的矩形,如下所示:

enter image description here

现在尝试用 UITextField 做同样的事情:

func findRect(forTextMatching match:String, in textField:UITextField) -> CGRect? {
    if let text = textField.text,
       let range = text.range(of: match)
    {
        let offset1 = text.distance(from: text.startIndex, to: range.lowerBound)
        let offset2 = text.distance(from: text.startIndex, to: range.upperBound)

        // beginningOfDocument is nil unless textField is first responder
        textField.becomeFirstResponder()
        defer {
            textField.resignFirstResponder()
        }

        if let pos1 = textField.position(from: textField.beginningOfDocument, offset: offset1),
            let pos2 = textField.position(from: textField.beginningOfDocument, offset: offset2)
        {
            if let textRange = textField.textRange(from: pos1, to: pos2)
            {
                textField.selectedTextRange = textRange
                var rect = textField.firstRect(for: textRange)

                return rect
            }
        }
    }

    return nil
}

第一个问题是 UITextField.beginningOfDocument 为 nil,除非该字段是第一响应者。这很容易处理,因此额外的行变成了 becomeFirstResonder 和 resignFirstResponder。

在测试应用程序中运行它并突出显示返回的矩形,如下所示:

enter image description here

返回的矩形略有偏移。



Best Answer-推荐答案


这似乎是 UITextField 的一个错误。在调试中检查 View 层次结构,在 UITextField 下有一个额外的 View ,类型为 UIFieldEditor,具有框架原点 (7,4)。返回的 rect 似乎与此 View 相关,而不是 UITextField。

原来你可以作为 UITextField 的 editRect 来访问这个框架。

UITextField 的修改函数:

func findRect(forTextMatching match:String, in textField:UITextField) -> CGRect? {
    if let text = textField.text,
       let range = text.range(of: match)
    {
        let offset1 = text.distance(from: text.startIndex, to: range.lowerBound)
        let offset2 = text.distance(from: text.startIndex, to: range.upperBound)

        // beginningOfDocument is nil unless textField is first responder
        textField.becomeFirstResponder()
        defer {
            textField.resignFirstResponder()
        }

        if let pos1 = textField.position(from: textField.beginningOfDocument, offset: offset1),
            let pos2 = textField.position(from: textField.beginningOfDocument, offset: offset2)
        {
            if let textRange = textField.textRange(from: pos1, to: pos2)
            {
                var rect = textField.firstRect(for: textRange)

                // firstRect is actually relative to editingRect, not textField
                let top = textField.editingRect(forBounds: textField.bounds).origin.y
                let left = textField.editingRect(forBounds: textField.bounds).origin.x
                rect = CGRect(x: rect.origin.x+left, y: rect.origin.y+top, width: rect.width, height: rect.height)

                return rect
            }
        }
    }

    return nil
}

产生正确的结果:

enter image description here

关于ios - 第一个矩形(对于 returns wrong value in UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910145/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4