检查是否启用了预测文本

时间:2014-09-23 11:29:23

标签: ios ios8

我想知道是否有任何方法可以检查其预测文本(键盘上方的灰色框)是否已启用。

当文本字段获得焦点时,我需要这样可以将视图更多地放到顶部。我得到了键盘的大小:CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

1 个答案:

答案 0 :(得分:9)

使用结束帧来获取键盘结束的最后位置。因此,在keyboardWillShow:通知回调中获取键盘结束帧。

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

    // Use keyboardEndFrame
}

当用户调整预测文本视图(缩小/展开)的大小时,这甚至可以工作,如果您有输入附件视图,也可以使用。

修改

这个问题标题的真正答案是不同的。截至目前,没有明显的方法可以确定是否启用了预测文本。所以我提出了一个解决方案,用不同的自动更正类型检查键盘框架。

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

用法(您可能只想检查一次)

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

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

    NSLog(@"Enabled: %d", isPredictiveTextEnabled);
}