如何禁用 iOS 8 表情符号键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25861468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable iOS 8 emoji keyboard?
提问by souvickcse
Is there any option to stop showing the emoji keyboard in iOS 8? It is not available in numpad and secure text but for email it is there. If it is not possible to disable it how to get the string values from the emoji?
是否有任何选项可以停止在 iOS 8 中显示表情符号键盘?它在小键盘和安全文本中不可用,但在电子邮件中可用。如果无法禁用它,如何从表情符号中获取字符串值?
回答by Yasika Patel
Try this:
尝试这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([textField isFirstResponder])
{
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage])
{
return NO;
}
}
return YES;
}
for more info see here.
有关更多信息,请参见此处。
EDIT :
编辑 :
You can hide emoji from Keyboard using this code:
您可以使用以下代码从键盘隐藏表情符号:
txtField.keyboardType=UIKeyboardTypeASCIICapable;
EDIT :
编辑 :
Hide emoji selection from keyboard using Interface builder.
使用界面生成器从键盘隐藏表情符号选择。
回答by Michael Voong
This works on iOS 7 and 8:
这适用于 iOS 7 和 8:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Disable emoji input
return [textField textInputMode] != nil;
}
回答by Leon
Just to update the great answers above for anyone facing the same issues as me:
只是为与我面临相同问题的任何人更新上面的好答案:
- An email field set as keyboardType = .EmailAddress still allows emojis
- You may have more than one condition you want to filter
- Swift
- Prevent C&P
- 设置为 keyboardType = .EmailAddress 的电子邮件字段仍然允许表情符号
- 您可能有多个要过滤的条件
- 迅速
- 防止 C&P
This is how we did it:
我们是这样做的:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if !string.canBeConvertedToEncoding(NSASCIIStringEncoding){
return false
}
//other conditons
}
Edit
编辑
Okay the solution with NSASCIIStringEncoding is no good if you want to allow certain characters for other languages.
好的,如果您想允许其他语言的某些字符,那么使用 NSASCIIStringEncoding 的解决方案是不好的。
Some letters blocked with NSASCIIStringEncoding:
一些字母被 NSASCIIStringEncoding 屏蔽:
- All other versions of a: àáa????ā
- All other versions of e: èéê?ē??
- All other versions of o: ??òó??ō?
- 所有其他版本的 a: àáa????ā
- 所有其他版本的 e: èéê?ē??
- 所有其他版本的 o: ??òó??ō?
Using NSASCIIStringEncoding is not a good idea if you want to support users from other countries!
如果你想支持来自其他国家的用户,使用 NSASCIIStringEncoding 不是一个好主意!
I also tried NSISOLatin1StringEncoding:
我也试过 NISOLatin1StringEncoding:
- Blocked versions of a: ā
- Blocked versions of e: ē??
- Blocked versions of o: ??
- a 的屏蔽版本:ā
- e 的屏蔽版本:ē??
- o 的阻塞版本:??
This is much better clearly.
这要清楚得多。
I also tried NSISOLatin2StringEncoding:
我也试过 NISOLatin2StringEncoding:
- Blocked versions of a: à???ā
- Blocked versions of e: èêē?
- Blocked versions of o: ò??ō?
- a 的屏蔽版本:à???ā
- 屏蔽版本的 e: èêē?
- o 的屏蔽版本:ò??ō?
Worse.
更差。
Edit2:
编辑2:
The following seems to work:
以下似乎有效:
extension String {
func containsEmoji() -> Bool {
for i in self.characters {
if i.isEmoji() {
return true
}
}
return false
}
}
extension Character {
func isEmoji() -> Bool {
return Character(UnicodeScalar(0x1d000)) <= self && self <= Character(UnicodeScalar(0x1f77f))
|| Character(UnicodeScalar(0x1f900)) <= self && self <= Character(UnicodeScalar(0x1f9ff))
|| Character(UnicodeScalar(0x2100)) <= self && self <= Character(UnicodeScalar(0x26ff))
}
}
Now we call:
现在我们调用:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if string.containsEmoji() {
return false
}
//other conditons
}
回答by dosdos
The solution that worked for me :
对我有用的解决方案:
self.texField.keyboardType = UIKeyboardTypeASCIICapable;
The emoji keyboard won't be available with that KeyboardType.
表情符号键盘不适用于该键盘类型。
But it's compulsory to check the kind of every char added in the textField, because the user can copy an emoji from elsewhere then paste it in.
但是必须检查文本字段中添加的每个字符的类型,因为用户可以从其他地方复制表情符号然后将其粘贴。
Here is how you could do so :
以下是您可以这样做的方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return [text canBeConvertedToEncoding:NSASCIIStringEncoding];
}
Be warned that, as I read in another post, this might also prevent user from using keyboard with special characters, like the Chinese one.
请注意,正如我在另一篇文章中所读到的,这也可能会阻止用户使用带有特殊字符的键盘,例如中文键盘。
回答by Kiran P Nair
In swift 3.0
在快速 3.0
textField.keyboardType = .asciiCapable
textField.keyboardType = .asciiCapable
回答by Yash Bedi
Though The Question is super old, I was Facing the same problem and was able to resolve it by the time this page loaded by this small trick :
虽然问题是超级古老的,但我面临着同样的问题,并且能够在通过这个小技巧加载此页面时解决它:
Simply Select "ASCII Capable" or "Numbers and Punctuations" in Interface Builder Xcode 8.2.1
只需在 Interface Builder Xcode 8.2.1 中选择“ASCII Capable”或“Numbers and Punctuations”
Output is No Emoji Keyboard =D
输出是没有表情符号键盘 =D
I'm sure it'll help Someone =)
我相信它会帮助某人 =)
回答by Sourabh Sharma
Swift 3.1 Solution
Swift 3.1 解决方案
// MARK: - Text Field Delegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return textField.textInputMode != nil
}
回答by Nghiem Dao
This is swift version.
这是快捷版。
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if textView.isFirstResponder() {
if textView.textInputMode?.primaryLanguage == nil || textView.textInputMode?.primaryLanguage == "emoji" {
return false;
}
}
return true;
}
回答by CodeBender
I wanted an answer for Swift 3.0 that would work on shouldChangeTextInRange
and came up with the following that is really simple and can handle anylanguage you throw at it (I include a photo using Chinese to demonstrate).
我想要一个适用于 Swift 3.0 的答案,shouldChangeTextInRange
并提出了以下非常简单的答案,并且可以处理您扔给它的任何语言(我附上了一张使用中文的照片来演示)。
The guard statement unwraps the optional text, and the return statement checks to see if it pulled out a value that satisfied the trimmingCharacters
check. If there is a value (meaning it is not part of the current devices's input language), then it will not be empty, so the method returns false and does not allow the character to be entered.
守卫语句解开可选文本,返回语句检查是否提取了满足trimmingCharacters
检查的值。如果有一个值(意味着它不是当前设备输入语言的一部分),那么它不会为空,因此该方法返回 false 并且不允许输入字符。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) else {
return true
}
return text.trimmingCharacters(in: CharacterSet.alphanumerics).isEmpty
}
This relies on Apple's CharacterSet.alphanumerics
, so there is no need to maintain and update a list of approved characters, unless you want to add or remove items.
这依赖于 Apple 的CharacterSet.alphanumerics
,因此无需维护和更新已批准的字符列表,除非您要添加或删除项目。
Yay for language agnostic!
是的,语言不可知!
Another common use is for email, since the default email keyboard provides emojis. I found with a small modification, I was able to restrict characters to what I wanted for an email address to work.
另一个常见用途是用于电子邮件,因为默认的电子邮件键盘提供表情符号。我发现通过一个小的修改,我能够将字符限制为我想要的电子邮件地址。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) else {
return true
}
return text.trimmingCharacters(in: CharacterSet.alphanumerics).isEmpty ||
!text.characters.filter { extension String {
func containsEmoji() -> Bool {
for scalar in unicodeScalars {
if !scalar.properties.isEmoji { continue }
return true
}
return false
}
}
== "@" || func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.containsEmoji() { return false }
return true
}
== "." }.isEmpty
}
回答by Arthur Stepanov
You can try this (Swift 5).
你可以试试这个(Swift 5)。
Create String extension:
创建字符串扩展:
##代码##Then use it in UITextFieldDelegate like this:
然后像这样在 UITextFieldDelegate 中使用它:
##代码##