iOS9之全面禁止emoji输入

SoccerZZM 2016-02-02

        收集了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

原创文章,转载请注明出处。

相关推荐