gpt4 book ai didi

ios - firstRect(对于 :) returns wrong value in UITextField

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:20 25 4
gpt4 key购买 nike

我正在尝试进行语法高亮显示以标记用户输入的错误。

遵循 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

返回的矩形略有偏移。

最佳答案

这似乎是 UITextField 的错误。在调试中检查 View 层次结构时,在 UITextField 下有一个额外的 View ,其类型为 UIFieldEditor,其框架原点 (7,4)。返回的矩形似乎与此 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 - firstRect(对于 :) returns wrong value in UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com