Подтвердить что ты не робот

Проверьте, включен ли интеллектуальный текст

Я хотел бы знать, есть ли способ проверить прогностический текст (серые поля над клавиатурой).

Мне нужно это, чтобы скопировать представление на несколько пикселей вверх, когда текстовое поле получает фокус. Я получаю размер клавиатуры: CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

4b9b3361

Ответ 1

Используйте конечный кадр, чтобы получить последнюю позицию, на которой закончится клавиатура. Поэтому в вашем обратном вызове уведомления 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);
}