iOS9之全面禁止emoji输入
收集了iOS9.2的所有emoji字符,禁止UITextView和UITextField的emoji字符输入。
#import <Foundation/Foundation.h> @interface StringUtils : NSObject + (void) deleteEmojiForTextView:(UITextView *)textView; + (void) deleteEmojiForTextField:(UITextField *)textField; @end
#import "StringUtils.h" @implementation StringUtils #define EMOJI @"...";//表情符号参考附件 + (void) deleteEmojiForTextView:(UITextView *)textView { NSString *string = textView.text; if(![self stringContainsEmoji:string]){ return; } __block NSString *noEmoji = string; [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock: ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { NSString *emojiEmotions = EMOJI; if([emojiEmotions rangeOfString:substring].location!=NSNotFound){ noEmoji = [noEmoji stringByReplacingOccurrencesOfString:substring withString:@""]; } }]; textView.text = noEmoji; } + (void) deleteEmojiForTextField:(UITextField *)textField { NSString *string = textField.text; if(![self stringContainsEmoji:string]){ return; } __block NSString *noEmoji = string; [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock: ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { NSString *emojiEmotions = EMOJI; if([emojiEmotions rangeOfString:substring].location!=NSNotFound){ noEmoji = [noEmoji stringByReplacingOccurrencesOfString:substring withString:@""]; } }]; textField.text = noEmoji; } + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; //__block NSString *noEmoji = string; [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock: ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { //const unichar hs = [substring characterAtIndex:0]; NSString *emojiEmotions = EMOJI; // NSLog(@"size=%ld",emojiEmotions.length); // NSLog(@"size2=%d",strlen([emojiEmotions UTF8String])); // NSLog(@"sub=%@",substring); if([emojiEmotions rangeOfString:substring].location!=NSNotFound){ NSLog(@"===>>>>>emoji"); returnValue = YES; *stop = YES; } }]; return returnValue; } @end
原创文章,转载请注明出处。
相关推荐
ailxxiaoli 2020-11-16
austindev 2020-06-28
唐文 2020-06-28
fujuan000 2020-04-30
Andrea0 2020-01-14
Trustport 2019-12-01
coinone 2019-10-29
boox 2018-10-25
TinyDolphin 2019-10-30
happinessaflower 2019-10-26
xuefeng0 2015-04-13
haixianTV 2015-08-30
yzhj00 2017-11-23
AxDoctor 2016-04-12
quanhaoH 2012-04-21
85206537 2015-09-19
88286530 2015-09-19
81246831 2015-08-24