JohnWickham
JohnWickham

Reputation: 561

Limit keyboard to Emoji

I have a text field that is only supposed to contain emoji characters. Is it possible to limit the user's keyboard to only emoji input, i.e. disable other keyboards?

Upvotes: 3

Views: 1900

Answers (2)

sumit kapoor
sumit kapoor

Reputation: 361

Currently am working on a project in which the requirement is same as you and trying to search the solution but did not find and I wrote a code that working fine for me. Hopefully it also works for you. I know its too late but helps you in future.

My Solution is

#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];
   }
 }

Upvotes: 1

jwknz
jwknz

Reputation: 6824

Keyboard types can be set in Interface Builder or programmatically using this guide:

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;

The Emoji Keyboard is set as a separate Language keyboard and therefore cannot be set programmatically. It is set by the user in the settings app.

I guess, even though it is cumbersome, you could either alert your user or have an help file inside your app to say that to have the best experience with your app they would need to enable that.

Also be aware that I believe Apple now rejects apps that create their own custom Emoji keyboard because it is a part of the main OS now.

Upvotes: 2

Related Questions