Autocorrect issue with a colored UITextview

2019-04-28 21:17发布

问题:

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?

回答1:

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!



回答2:

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;
}