Autocorrect issue with a colored UITextview

∥☆過路亽.° 提交于 2019-12-07 03:21:08

问题


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


来源:https://stackoverflow.com/questions/13684167/autocorrect-issue-with-a-colored-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!