首先在代理方法写上:
// 禁止输入表情
BOOL emoji= [self stringContainsEmoji:self.addTeamsNameField.text];
if (emoji){
//最后输入的是表情截取掉
self.addTeamsNameField.text = [self.addTeamsNameField.text substringToIndex:self.addTeamsNameField.text.length -2];
}
if ([self isInputRuleNotBlank:string] || [string isEqualToString:@""]) {//当输入符合规则和退格键时允许改变输入框
return YES;
} else {
return NO;
}
另外写入一下三个方法:
//是否包含表情符
+ (BOOL)stringContainsEmoji:(NSString *)string
{
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
/**
* 字母、数字、中文正则判断(不包括空格)
*/
- (BOOL)isInputRuleNotBlank:(NSString *)str {
NSString *pattern = @"[➋➌➍➎➏➐➑➒\0-9a-zA-Z\u4e00-\u9fa5\\.\\*\\)\\(\\+\\$\\[\\?\\\\\\^\\{\\|\\]\\}%%%@\'\",。‘、-【】·!_——=:;;<>《》‘’“”!#~!#¥%……&*()]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}
- (void)textFieldDidChange:(NSNotification *)obj{
UITextView *textField = obj.object;
NSString *toBeString = textField.text;
NSString *lang = [[textField textInputMode] primaryLanguage]; // 获取当前键盘输入模式
if([lang hasPrefix:@"zh-Hans"]) {
UITextRange *selectedRange = [textField markedTextRange];
//获取高亮部分
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//没有高亮选择的字,则对已输入的文字进行字数统计和限制
if(!position) {
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text = getStr;
}
}
} else{
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text= getStr;
}
}
}
/**
* 获得 kMaxLength长度的字符
*/
-(NSString *)getSubString:(NSString*)string
{
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData* data = [string dataUsingEncoding:encoding];
NSInteger length = [data length];
if (length > 32) {
NSData *data1 = [data subdataWithRange:NSMakeRange(0, 32)];
NSString *content = [[NSString alloc] initWithData:data1 encoding:encoding];
if (!content || content.length == 0) {
data1 = [data subdataWithRange:NSMakeRange(0, 32 - 1)];
content = [[NSString alloc] initWithData:data1 encoding:encoding];
}
return content;
}
return nil;
}