Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if predictive text is enabled

Tags:

ios

ios8

I would like to know if there is any way to check wether predictive text (the grey boxes above the keyboard) are enabled.

I need this to scoll a view a few pixel more to the top when the textfield gets its focus. I get the size of the keyboard with: CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

like image 322
palme Avatar asked Sep 23 '14 11:09

palme


People also ask

How do I confirm my predictive text?

Accept or reject a predictive text suggestion While typing text, you can do either of the following: Accept a suggested word or emoji by tapping it; accept a highlighted suggestion by entering a space or punctuation. When you tap a suggested word, a space appears after the word.

Where is the predictive text button?

If you're using an Android, the language varies, but you should find settings for your keyboard in Settings > General > Language and Input > Keyboard Preferences (you might have to pick a keyboard) > Text Correction (might be called Word Suggestion). Tap the switch to toggle it on.

Why can't I turn on my predictive text?

@1Papillon: To troubleshoot your issue please go to Settings > General Management > Language and Input > On-Screen Keyboard > Samsung Keyboard > Smart Typing > Make Sure that Predictive Text is toggled on > Back > About Samsung Keyboard > Tap the 'i' in the top right > Storage > Clear Cache > Clear Data > Restart your ...


1 Answers

Use end frame to get the last position the keyboard will end up. So in your keyboardWillShow: notification callback get the keyboard end frame.

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.object;
    CGRect keyboardEndFrame;
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    // Use keyboardEndFrame
}

This even works when user resizes the predictive text view (shrink/expand) and also works if you have an input accessory view.

Edit

The real answer to the title of this question is different. As of now, there is no obvious way to find out if the predictive text is enabled. So I came up a solution which checks keyboard frame with different autocorrection types.

ZSTKeyboardChecker.h

#import <UIKit/UIKit.h>

@interface ZSTKeyboardChecker : NSObject

- (BOOL)isPredictiveTextEnabledForTextField:(UITextField *)textField;

@end

ZSTKeyboardChecker.m

@interface ZSTKeyboardChecker ()

@property (assign, nonatomic) CGRect keyboardEndFrame;

@end

@implementation ZSTKeyboardChecker

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
    return self;
}

- (BOOL)isPredictiveTextEnabledForTextField:(UITextField *)textField
{
    if (textField.autocorrectionType == UITextSpellCheckingTypeNo) {
        return NO;
    }

    BOOL isFirstResponder = [textField isFirstResponder];
    BOOL autoCorrectionType = [textField autocorrectionType];

    [textField resignFirstResponder];

    // Get the frame with possibly including predictive text
    [textField becomeFirstResponder];
    CGRect predictiveKeyboardEndFrame = self.keyboardEndFrame;
    [textField resignFirstResponder];

    // Get the keyboard frame without predictive text
    textField.autocorrectionType = UITextSpellCheckingTypeNo;
    [textField becomeFirstResponder];
    CGRect defaultKeyboardEndFrame = self.keyboardEndFrame;
    [textField resignFirstResponder];

    // Restore state
    textField.autocorrectionType = autoCorrectionType;
    if (isFirstResponder) {
        [textField becomeFirstResponder];
    }

    BOOL isPredictiveTextEnabled = !CGPointEqualToPoint(predictiveKeyboardEndFrame.origin, defaultKeyboardEndFrame.origin);
    return isPredictiveTextEnabled;
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.object;
    CGRect keyboardEndFrame;
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    self.keyboardEndFrame = keyboardEndFrame;
}

@end

Usage (You may want to check it only once)

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    ZSTKeyboardChecker *keyboardChecker = [[ZSTKeyboardChecker alloc] init];
    BOOL isPredictiveTextEnabled = [keyboardChecker isPredictiveTextEnabledForTextField:self.textField];

    NSLog(@"Enabled: %d", isPredictiveTextEnabled);
}
like image 72
irmakcanozsut Avatar answered Sep 17 '22 17:09

irmakcanozsut