检查是否启用了预测文本

问题描述 投票:0回答:1

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

当文本字段获得焦点时,我需要将视图滚动到顶部几个像素。我通过以下方式获取键盘的大小:

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

ios ios8
1个回答
10
投票

使用结束框架获取键盘最终到达的位置。因此,在您的

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.