iOStextfield 限制輸入字元長度和過濾Emoji,
//增加修改框
-(void)creatSaveView{
UILabel * lab = [[UILabel alloc]init];
if (self.searchType == 1) {
lab.frame = CGRectMake(14, 8 * KWIDTH + 64, IPHONE_WIDTH - 100, 16 *KWIDTH);
}else{
lab.frame = CGRectMake(14, 8 * KWIDTH, IPHONE_WIDTH - 100, 16 * KWIDTH);
}
lab.text = @"備忘名";
lab.font = [UIFont systemFontOfSize:14];
lab.textColor = kColor(137, 137, 137);
[self.view addSubview:lab];
UIView *view=[[UIView alloc] init];
if (self.searchType == 1) {
view.frame = CGRectMake(0, 32*KWIDTH + 64, IPHONE_WIDTH, 40*KWIDTH);
}else{
view.frame = CGRectMake(0, 32*KWIDTH, IPHONE_WIDTH, 40*KWIDTH);
}
UIView *lineViewlight3=[[UIView alloc] initWithFrame:CGRectMake(0,0,IPHONE_WIDTH, 0.5)];
lineViewlight3.backgroundColor=kColor(200,200 , 200);
lineViewlight3.alpha=0.8;
[view addSubview:lineViewlight3];
UIView *lineViewlight4=[[UIView alloc] initWithFrame:CGRectMake(0,40*KWIDTH-0.5, IPHONE_WIDTH, 0.5)];
lineViewlight4.backgroundColor=kColor(200,200 , 200);
lineViewlight4.alpha=0.7;
[view addSubview:lineViewlight4];
view.backgroundColor=[UIColor whiteColor];
_nameField = [[UITextField alloc]initWithFrame:CGRectMake(10*KWIDTH, 0, IPHONE_WIDTH-15*KWIDTH, 40*KWIDTH)];
_nameField.text = self.NameField;
_nameField.borderStyle = UITextBorderStyleNone;
_nameField.clearButtonMode= UITextFieldViewModeAlways;
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:_nameField];
[view addSubview:_nameField];
[self.view addSubview:view];
}
-(void)textFiledEditChanged:(NSNotification*)obj{
UITextField *textField = (UITextField *)obj.object;
//過濾掉系統資料表情
// UITextView * textView;
//
// NSRange textRange = [textView selectedRange];
// [textView setText:[self disable_emoji:[textView text]]];
// [textView setSelectedRange:textRange];
NSString *toBeString = textField.text;
DSLog(@"-----toBeString 1 ----%@",toBeString);
BOOL isEmoj = [self stringContainsEmoji:toBeString];
NSString * _showStr;
toBeString = [self disable_emoji:toBeString];
// textField.text = toBeString;
DSLog(@"-----toBeString 2 -----%@",toBeString);
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; //鍵盤輸入模式
if([lang isEqualToString:@"zh-Hans"]) { //簡體中文輸入,包括簡體拼音,健體五筆,簡體手寫
UITextRange *selectedRange = [textField markedTextRange];
//擷取高亮部分
UITextPosition *position = [textFieldpositionFromPosition:selectedRange.start offset:0];
//沒有高亮選擇的字,則對已輸入的文字進行字數統計和限制
if(!position) {
if(toBeString.length > kHZMaxLength) {
textField.text = [toBeString substringToIndex:kHZMaxLength];
_showStr = [toBeString substringToIndex:kHZMaxLength];
}
}
//有高亮選擇的字串,則暫不對文字進行統計和限制
else{
}
}
//中文IME以外的直接對其統計限制即可,不考慮其他語種情況
else{
if(toBeString.length > kEGMaxLength) {
textField.text= [toBeString substringToIndex:kEGMaxLength];
_showStr = [toBeString substringToIndex:kEGMaxLength];
}
}
DSLog(@" _showStr %@",_showStr);
if (isEmoj) {
if ([_showStr length]) {
textField.text = _showStr;
}else{
textField.text = toBeString;
}
}
}
//判斷NSString字串是否包含emoji表情
- (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];
// surrogate pair
if (0xd800) {
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 {
// non surrogate
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;
}
#pragma Mark --- 過濾表情
- (NSString *)disable_emoji:(NSString *)text
{
NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:@"[^\\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;
}