有效的禁止文本输入框输入表情


#import "ViewController.h"
 
@implementation ViewController
{
     IBOutlet UITextView *textinput;
}
 
- ( void )viewDidLoad
{
     [ super viewDidLoad ];
     [textinput setDelegate : self ];
}
 
- ( void )textViewDidChange:( UITextView *)textView
{
     NSRange textRange = [textView selectedRange ];
     [textView setText :[ self disable_emoji :[textView text ]]];
     [textView setSelectedRange :textRange];
}
 
- ( NSString *)disable_emoji:( NSString *)text
{
     NSRegularExpression *regex = [ NSRegularExpression regularExpressionWithPattern : @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options : NSRegularExpressionCaseInsensitive error :nil ];
     NSString *modifiedString = [regex stringByReplacingMatchesInString :text
                                                               options : 0
                                                                 range :NSMakeRange( 0 , [text length ])
                                                          withTemplate : @"" ];
     return modifiedString;
}
 

//文本输入框禁止方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    
    if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
    
}
@end

你可能感兴趣的:(UITextField相关)