将键盘限制为表情符号

时间:2012-12-22 18:32:25

标签: iphone textfield emoji

我有一个文本字段,只能包含表情符号字符。是否可以将用户的键盘限制为只有表情符号输入,即禁用其他键盘?

2 个答案:

答案 0 :(得分:2)

键盘类型可以在Interface Builder中设置,也可以使用本指南以编程方式设置:

typedef enum {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

  } UIKeyboardType;

表情符号键盘设置为单独的语言键盘,因此无法以编程方式设置。它由用户在设置应用中设置。

我想,即使它很麻烦,你也可以提醒你的用户或在你的应用程序中有一个帮助文件,以便说明要获得最佳的应用程序体验,他们需要启用它。

另外请注意,我相信Apple现在拒绝创建自己的自定义表情符号键盘的应用程序,因为它现在是主操作系统的一部分。

答案 1 :(得分:0)

目前我正在开发一个项目,其中的要求与您相同,并尝试搜索解决方案但没有找到,我编写了一个对我来说工作正常的代码。希望它也适合你。我知道它太晚了,但将来会帮助你。

我的解决方案是

#pragma mark validate emoji's

-(void)validateEmoji
{
BOOL lowerCaseLetter=0,upperCaseLetter= 0,digit=0,specialCharacter=0;
    for (int i = 0; i < [emojiTextField.text length]; i++)
    {
        unichar c = [emojiTextField.text characterAtIndex:i];
        if(!lowerCaseLetter)
        {
          lowerCaseLetter = [[NSCharacterSet     lowercaseLetterCharacterSet] characterIsMember:c];
        }
        if(!upperCaseLetter)
        {
            upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
        }
        if(!digit)
        {
            digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
        }
        if(!specialCharacter)
        {
            specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
        }
    }

    if(lowerCaseLetter || upperCaseLetter || digit || specialCharacter)
    {
        // if textfield contains special characters,lowercase,uppercase,numeric digits
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Please select only emoji's from Keyboard"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        emojiTextField.text=@"";
        [emojiTextField becomeFirstResponder];
        emojiTextFieldFlag = false;
    }
    else
    {
        emojiTextFieldFlag = true;
        // emoji is selected do whatever you want.
    }

}

#pragma mark TextField delegate methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField
 {
if ([textField isEqual:emojiTextField])
{
    [emojiTextField resignFirstResponder];
    NSLog(@"%@",emojiTextField.text);
    // make it blank for next click when comes back
    appdelegate.appdelegateEmojiTextFieldClicked =@"";
    if (emojiTextFieldFlag)
    {
        [self saveEmojiJsonParsing];
    }
    else
    {
        [emojiTextField becomeFirstResponder];
    }
  }
  return YES;
  }

-(void)textFieldDidEndEditing:(UITextField *)textField
 {
   if ([textField isEqual:emojiTextField])
   {
    [self validateEmoji];
   }
 }