Basillius2
Basillius2

Reputation: 61

Autocorrect issue with a colored UITextview

I'm using in my app a textview with blue colored background.

When autocorrect is enabled, whenever it corrects a word a white box forms around the word and the text's color changes also.

Any ideas on how to stop this effect?

Upvotes: 6

Views: 661

Answers (2)

hartwellalex
hartwellalex

Reputation: 406

This may have other side effects I am currently investigating what else calls this, but overriding firstRect(for range) in UITextView and returning CGrect.Zero removes the highlight.

override public func firstRect(for range: UITextRange) -> CGRect {
     return CGRect.zero;
}

Upvotes: 0

Tieme
Tieme

Reputation: 65369

Nope, I think you can't change the color of the overlay or the textcolor out of the box..

This is a hard one, if you put a breakpoint in - (void) layoutSubviews {..} of your view you'll see UIKit has drawn a 'UITextSelectionView' above the text...

I tried to subclass UITextView to see if its layoutSubviews get's fired and it seems it does!

So I tried to remove the overlay:

- (void) layoutSubviews {
    [super layoutSubviews];

    for (UIView *subview in self.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITextSelectionView"]) {
            DLog(@"Subview %@", [subview debugDescription]);
            [subview removeFromSuperview];
            break;
        }
    }
}

And it worked.. but that results in missing the cursor, and disabling any selection :(

I think you'll have to disable autocorrect..

I'll re-start the bounty if someone finds a real answer!

Upvotes: 4

Related Questions