'Turn off Autocorrect Globally in an App
I would like to disable text-entry autocorrect in an iPad application, regardless of what the global settings for autocorrect are on the device. Is there a good way to do this through the API, or will I simply need to go through the whole app, find all text entry fields, and turn the option off for each field individually?
Solution 1:[1]
Im sorry but you have to go trough all the text fields and disable it
Solution 2:[2]
You can override the default text field autocorrection type with a bit of method swizzling. In your app delegate or somewhere else sensible:
#import <objc/runtime.h>
// Prevent this code from being called multiple times
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
// (Re)implement `-[UITextField autocorrectionType]` to return `UITextAutocorrectionTypeNO`.
IMP noAutocorrectionTypeIMP = imp_implementationWithBlock(^(UITextField *_self){ return UITextAutocorrectionTypeNo; });
class_replaceMethod([UITextField class], @selector(autocorrectionType), noAutocorrectionTypeIMP, autocorrectionTypeMethodDescription.types);
});
Solution 3:[3]
You can probably subclass UITextField and set your desired properties to it. Instead of the UITextField you can use this subclassed version.
This may be worthy if you haven't started implementing your application yet!
Solution 4:[4]
as @cocoakomali
suggested, you can create a category of UITextField
to disable autocorrect for all UITextField
in the app by default
@implementation UITextField (DisableAutoCorrect)
- (instancetype)init {
self = [super init];
if (self) {
[self setAutocorrectionType:UITextAutocorrectionTypeNo];
}
return self;
}
@end
Solution 5:[5]
You can't disable globally but what you can do is extend UIViewController to loop on all textfields and disable autocorrect
Extension :
extension UIViewController {
func removeAutocorrect() {
self.view.allSubviews.forEach { v in
if let textField = v as? UITextField {
textField.autocorrectionType = .no
}
}
}
}
Usage :
class MyViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Do other stuff here
//Must be called after adding all your TextField subviews
self.removeAutocorrect()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antonio MG |
Solution 2 | |
Solution 3 | cocoakomali |
Solution 4 | JK ABC |
Solution 5 | Adrien Roux |