Ios 防止在UITextView中捕获非ASCII字符

Ios 防止在UITextView中捕获非ASCII字符,ios,objective-c,Ios,Objective C,我有一个只支持ASCII字符集的传统数据库列。我需要一个方法,防止非ASCII字符被键入或粘贴到UITextView中。我需要过滤掉表情符号和所有其他unicode字符。这有两个部分。首先,通过适当设置键盘类型,防止首先键入非ASCII字符: textView.keyboardType = UIKeyboardTypeASCIICapable; 其次,通过实现此委托方法,防止从其他应用程序粘贴非ASCII字符: - (BOOL)textView:(UITextView *)textView s

我有一个只支持ASCII字符集的传统数据库列。我需要一个方法,防止非ASCII字符被键入或粘贴到UITextView中。我需要过滤掉表情符号和所有其他unicode字符。

这有两个部分。首先,通过适当设置键盘类型,防止首先键入非ASCII字符:

textView.keyboardType = UIKeyboardTypeASCIICapable;
其次,通过实现此委托方法,防止从其他应用程序粘贴非ASCII字符:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // trim any non-ASCII characters
    NSString* s = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithRange:NSMakeRange(0, 128)].invertedSet] componentsJoinedByString:@""];

    // manually replace the range in the textView
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:s];

    // prevent auto-replacement
    return NO;
}

这有两个部分。首先,通过适当设置键盘类型,防止首先键入非ASCII字符:

textView.keyboardType = UIKeyboardTypeASCIICapable;
其次,通过实现此委托方法,防止从其他应用程序粘贴非ASCII字符:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // trim any non-ASCII characters
    NSString* s = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithRange:NSMakeRange(0, 128)].invertedSet] componentsJoinedByString:@""];

    // manually replace the range in the textView
    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:s];

    // prevent auto-replacement
    return NO;
}

这就是我在UITextField中使用的,只是一个替代解决方案,它可以在默认键盘和自定义键盘上工作,并禁用emojis的复制粘贴。无需将键盘类型设置为ascii,因为它只会禁用默认iOS键盘的表情

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    //This is universal solution works on default as well as custom keyboards like SwiftKeyplus it will diallow pasting emoji
    if !string.canBeConvertedToEncoding(NSASCIIStringEncoding)
    {
        return false
    }

    return true
}

这就是我在UITextField中使用的,只是一个替代解决方案,它可以在默认键盘和自定义键盘上工作,并禁用emojis的复制粘贴。无需将键盘类型设置为ascii,因为它只会禁用默认iOS键盘的表情

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    //This is universal solution works on default as well as custom keyboards like SwiftKeyplus it will diallow pasting emoji
    if !string.canBeConvertedToEncoding(NSASCIIStringEncoding)
    {
        return false
    }

    return true
}

Swift 4清洁代码解决方案

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return string.canBeConverted(to: .ascii)
}

Swift 4清洁代码解决方案

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return string.canBeConverted(to: .ascii)
}

这是失败的。使用此方法,我可以在UITextField中输入一个空的重音字符,如下所示:''-'此操作失败。使用此方法,我可以在UITextField中输入一个空的重音字符,如下所示:''-'此操作失败。使用此方法,我可以在UITextField中输入一个空的重音字符,如下所示:''-'此操作失败。使用此方法,我可以在UITextField中输入一个空的重音字符,如下所示:`