【发布时间】:2020-01-27 09:42:06
【问题描述】:
我有一个UITextField 子类,我在其中覆盖左视图、文本和插入符号的绘图矩形。这是使用leftViewRect(forBounds:)、leftViewRect(forBounds:)、caretRect(for:) 等完成的。一切都按预期工作。
问题在于,因为我在caretRect(for:) 中将绘图矩形(向上)移动了-2 个点,所以所选文本的绘图矩形比它应该的低2 个点。我尝试覆盖selectionRects(for:),但UITextSelectionRect 的rect 是一个只能获取的属性。实现这一目标的正确方法是什么?
代码
public override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
super.selectionRects(for: range).map {
var selectionRect = $0
selectionRect.rect.origin.y -= 2 // Left side of mutating operator isn't mutable: 'rect' is a get-only property
return selectionRect
}
}
【问题讨论】:
-
来自
UITextSelectionRect的文档:如果您正在实现自定义文本输入视图,您可以继承并使用您的自定义类来返回与选择相关的信息。子类化时,您应该覆盖并重新实现所有属性。在您的自定义实现中,不要调用 super.. -
谢谢,我看到了。我仍然不确定如何确切地得到我想要的。我会使用来自
super.selectionRects(for: range)的映射值初始化UITextSelectionRect子类,其中rect属性的y 值被修改?一些示例代码会很有帮助。 -
这似乎是你需要的。
标签: swift uitextfield uikit selection