I have a UITextField
subclass where I'm overriding the drawing rectangles for the left view, text and caret. This is done using leftViewRect(forBounds:)
, leftViewRect(forBounds:)
, caretRect(for:)
, etc. Everything works as expected.
The problem is that because I'm moving the drawing rectangle (up) by -2 points in caretRect(for:)
, the drawing rectangle for selected text is 2 points lower than it should be. I tried overriding selectionRects(for:)
, but UITextSelectionRect
's rect
is a get-only property. What's the correct way to achieve this?
Code
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
}
}