//增加修改框
-(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{
}
}
//中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
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;
}