我有一个文本字段,只应包含表情符号。是否可以限制用户的键盘仅使用表情符号输入,即禁用其他键盘?
可以使用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;
表情符号键盘被设置为单独的语言键盘,因此无法通过编程进行设置。它是由用户在设置应用程序中设置的。
我想,即使这很麻烦,您也可以向用户发出警报或在应用程序内提供帮助文件,以说明要获得最佳体验,他们需要启用该功能。
另外,请注意,我相信苹果现在拒绝创建自己的自定义表情符号键盘的应用程序,因为它已成为主要操作系统的一部分。
目前我正在开发一个项目,其中需求与您相同,并尝试寻找解决方案,但未找到。我编写了一段代码,对我来说运行良好。希望它也能为您提供帮助。我知道现在可能已经太晚了,但这可以帮助您在将来。
我的解决方案是
#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];
}
}