За комментарий rmaddy и этот ответ , я решил это путем создания подкласса UITextSelectionRect
и передачи сопоставленных значений из super.selectionRects(for: range)
с измененным rect
.Вот на что я приземлился:
public final class MyUITextField: UITextField {
public override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
super.selectionRects(for: range).map {
MyTextSelectionRect(
rect: CGRect(x: $0.rect.origin.x, y: $0.rect.origin.y - 2, width: $0.rect.width, height: $0.rect.height),
writingDirection: $0.writingDirection,
containsStart: $0.containsStart,
containsEnd: $0.containsEnd,
isVertical: $0.isVertical
)
}
}
//...
}
public final class MyTextSelectionRect: UITextSelectionRect {
public override var rect: CGRect { _rect }
public override var writingDirection: NSWritingDirection { _writingDirection }
public override var containsStart: Bool { _containsStart }
public override var containsEnd: Bool { _containsEnd }
public override var isVertical: Bool { _isVertical }
private let _rect: CGRect
private let _writingDirection: NSWritingDirection
private let _containsStart: Bool
private let _containsEnd: Bool
private let _isVertical: Bool
public init(
rect: CGRect,
writingDirection: NSWritingDirection,
containsStart: Bool,
containsEnd: Bool,
isVertical: Bool
) {
_rect = rect
_writingDirection = writingDirection
_containsStart = containsStart
_containsEnd = containsEnd
_isVertical = isVertical
}
}