简体   繁体   English

如何禁用 iOS 8 表情符号键盘?

[英]How to disable iOS 8 emoji keyboard?

Is there any option to stop showing the emoji keyboard in iOS 8?是否有任何选项可以停止在 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?如果无法禁用它,如何从表情符号中获取字符串值?

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.使用界面生成器从键盘隐藏表情符号选择。

在此处输入图片说明

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

Just to update the great answers above for anyone facing the same issues as me:只是为与我面临相同问题的任何人更新上面的好答案:

  1. An email field set as keyboardType = .EmailAddress still allows emojis设置为 keyboardType = .EmailAddress 的电子邮件字段仍然允许表情符号
  2. You may have more than one condition you want to filter您可能有多个要过滤的条件
  3. Swift迅速
  4. Prevent C&P防止 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: èéêëēėę e 的所有其他版本: èéêëēėę
  • All other versions of o: ôöòóœøōõ 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: ā a 的屏蔽版本:ā
  • Blocked versions of e: ēėę e 的屏蔽版本:ēėę
  • Blocked versions of o: œø o 的屏蔽版本:œø

This is much better clearly.这要清楚得多。

I also tried NSISOLatin2StringEncoding:我也试过 NISOLatin2StringEncoding:

  • Blocked versions of a: àæãåā a 的屏蔽版本:àæãåā
  • Blocked versions of e: èêēė e 的屏蔽版本:èêēė
  • Blocked versions of o: òœøōõ 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
}

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.请注意,正如我在另一篇文章中所读到的,这也可能会阻止用户使用带有特殊字符的键盘,例如中文键盘。

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 =)我相信它会帮助某人 =)

在快速 3.0

textField.keyboardType = .asciiCapable

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
    }

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

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 any language you throw at it (I include a photo using Chinese to demonstrate).我想要一个适用于shouldChangeTextInRange Swift 3.0 答案,并提出了以下非常简单的答案,可以处理您扔给它的任何语言(我附上了一张使用中文的照片来演示)。

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.守卫语句解开可选文本,返回语句检查是否提取了满足trimmingCharacters检查的值。 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.如果有一个值(意味着它不是当前设备输入语言的一部分),那么它不会为空,因此该方法返回 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 { $0 == "@" || $0 == "." }.isEmpty
}

For those who require international keyboards, this has worked well for iOS 9 and above:对于那些需要国际键盘的人来说,这在 iOS 9 及更高版本上运行良好:

  1. Add the following extension function to check if a string contains emoji:添加以下扩展函数以检查字符串是否包含表情符号:

     extension String { var containsEmoji: Bool { for scalar in unicodeScalars { switch scalar.value { case 0x3030, 0x00AE, 0x00A9,// Special Characters 0x1D000...0x1F77F, // Emoticons 0x2100...0x27BF, // Misc symbols and Dingbats 0xFE00...0xFE0F, // Variation Selectors 0x1F900...0x1F9FF: // Supplemental Symbols and Pictographs return true default: continue } } return false } }
  2. Another extension function to remove the emoji's from the string:从字符串中删除表情符号的另一个扩展函数:

     extension String { var minusEmojis: String { let emojiRanges = [0x1F601...0x1F64F, 0x2702...0x27B0, 0x1D000...0x1F77F, 0x2100...0x27BF, 0xFE00...0xFE0F, 0x1F900...0x1F9FF] let emojiSet = Set(emojiRanges.joined()) return String(self.filter { !emojiSet.contains($0.unicodeScalarCodePoint) }) } }
  3. Add a target to your text field like this:将目标添加到您的文本字段,如下所示:

     textField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
  4. Finally remove any emoji present from the target selector:最后从目标选择器中删除任何表情符号:

     @objc func didChangeText(field: UITextField) { if (field.text?.containsEmoji == true) { field.text = field.text?.minusEmojis } }

Hope this helps someone!希望这可以帮助某人! Cheers.干杯。

You can try this (Swift 5).你可以试试这个(Swift 5)。

Create String extension:创建字符串扩展:

extension String {

    func containsEmoji() -> Bool {
        for scalar in unicodeScalars {
            if !scalar.properties.isEmoji { continue }
            return true
        }

        return false
    }

}

Then use it in UITextFieldDelegate like this:然后像这样在 UITextFieldDelegate 中使用它:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     if string.containsEmoji() { return false }
     return true
}

In Swift : - It Will restrict the to past the emoji in your TextView and Just change the keyboard type to ascii capable在 Swift 中: - 它会限制你的 TextView 中的表情符号,只需将键盘类型更改为 ascii 功能

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    var result = Bool()

            let disallowedCharacterSet1 = NSCharacterSet.symbolCharacterSet()
            let replacementStringIsLegal = text.rangeOfCharacterFromSet(disallowedCharacterSet1) == nil
            result = replacementStringIsLegal


        let newLength = textView.text!.utf16.count + text.utf16.count - range.length
        return newLength <= 300 && result // Bool

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM