oscarimonbox
2/15/2017 - 8:45 AM

No escribir emojis en textfield

No escribir emojis en textfield

Sacado de aquí:
http://stackoverflow.com/questions/8664134/ios-5-how-can-i-disable-emoji-keyboard-in-my-application
http://stackoverflow.com/questions/25861468/how-to-disable-ios-8-emoji-keyboard

#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

0) SI ES POSIBLE, poner que el campo sea del tipo ASCII CAPABLE (eso impide que se pueda seleccionar el teclado de emojis). Aunque para los casos de campos email o numericos no se podrá.

1) En el BASE: 
- (BOOL)isValidString: (NSString*) text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:text options:NSMatchingWithTransparentBounds range:NSMakeRange(0, [text length])];
    
    if (numberOfMatches > 0) {
        return NO;
    }
    
    return YES;
}


2) 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (![self isValidString:string]){
        return NO;
    }

    if (IS_OS_7_OR_LATER) {
        if ([textField isFirstResponder]) {
            if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { // In fact, in iOS7, '[[textField textInputMode] primaryLanguage]' is nil
                return NO;
            }
        }
    } else {
        if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"emoji"] ) {
            return NO;
        }
    }
    
    return YES;
}

3) Si es searchBar

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if (![self isValidString:text]){
        return NO;
    }
    
        
    if (IS_OS_7_OR_LATER) {
        if ([searchBar isFirstResponder]) {
            if ([[[searchBar textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[searchBar textInputMode] primaryLanguage]) { // In fact, in iOS7, '[[textField textInputMode] primaryLanguage]' is nil
                return NO;
            }
        }
    } else {
        if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"emoji"] ) {
            return NO;
        }
    }
    
    return YES;
    
}