UITextField Caret stops moving around when too large

125 views Asked by At

UITextField allows you to override -caretRectForPosition: in subclasses. I want the caret to cover the text field's entire height.

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    CGRect inherited = [super caretRectForPosition:position];
    inherited.origin.y = -50.0;
    inherited.size.height = 200.0;

    return inherited;
}

If I make it too large however (like in the example), it will stop moving around with the magnifying glass, effectively making the user unable to switch to another part of the text.

Why is that and how can I address the problem?

1

There are 1 answers

0
Christian Schnorr On BEST ANSWER

I figured this was only an issue when trying to make the caret way larger than the text field was. Making it cover the only entire editing rect worked fine.

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    CGRect const inherited = [super caretRectForPosition:position];
    CGRect const rect = ({
        CGRect rect = inherited;
        rect.origin.y = 0.0;
        rect.size.height = CGRectGetHeight([self editingRectForBounds:self.bounds]);
        rect;
    });

    return rect;
}

Bonus: In case you're trying to get the selection range view to be the same height, take a look at -selectionRectsForRange: and adjust the size of all rects accordingly. You will need to subclass UITextSelectionRect to make it mutable though.