iOS 5: How can I disable Emoji keyboard in my application?

佐手、 提交于 2019-12-17 23:37:37

问题


I don't want the Emoji keyboard allowance in my application so I want to disable it only in my application. There is one way to do it by applying the answer from this link:

Making An Emoji Enabeling App

But this would not work on iOS 5 (iOS 4.3 do work). Is there any way to disable Emoji keyboard in iOS 5. Thank you.


回答1:


You can simply set the property keyboardType of the UITextField or UITextView to UIKeyboardTypeASCIICapable. This disables the Emoji Keyboard for this UI element.




回答2:


@mschluepmann, But set UIKeyboardTypeASCIICapable can not input Chinese

And you can do it like below

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    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;
}

But sometimes, the emoji may not entered by emoji keyboard. For example, when you type "哈哈" it shows 😄 emoji on the header of the keyboard. In the case, the code above will make no effect. So you should do a twice validation as following:

- (BOOL)isValidString
{
    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:self options:NSMatchingWithTransparentBounds range:NSMakeRange(0, [self length])];

    if (numberOfMatches > 0) {
        return NO;
    }

    return YES;
}



回答3:


@Lapinou's answer helped me, repost his category of NSString on github: NSString-RemoveEmoji



来源:https://stackoverflow.com/questions/8664134/ios-5-how-can-i-disable-emoji-keyboard-in-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!