+ (void)GBSpaceConstraintsLimitString:(id)Textfield
limitMaxWord:(NSInteger)limitMaxWord{
if ([Textfield isKindOfClass:UITextField.class]) {
if ([(UITextField *)Textfield text].length > limitMaxWord) {
UITextRange *markedRange = [Textfield markedTextRange];
if (markedRange) {
return;
}
//Emoji占2个字符,如果是超出了半个Emoji,用15位置来截取会出现Emoji截为2半
//超出最大长度的那个字符序列(Emoji算一个字符序列)的range
NSString * temText = @"";
NSString * checker = [NSString stringWithFormat:@"%C", 8198]; // 英文输入时的字符集
if ([[(UITextField *)Textfield text] rangeOfString:checker].length) {
temText = [[(UITextField *)Textfield text] stringByReplacingOccurrencesOfString:checker withString:@""];
}else{
temText = [(UITextField *)Textfield text];
}
NSRange range = [temText rangeOfComposedCharacterSequenceAtIndex:limitMaxWord];
((UITextField *)Textfield).text = [temText substringToIndex:range.location];
}
}else if ([Textfield isKindOfClass:UITextView.class]){
if ([(UITextView *)Textfield text].length > limitMaxWord) {
UITextRange *markedRange = [Textfield markedTextRange];
if (markedRange) {
return;
}
//Emoji占2个字符,如果是超出了半个Emoji,用15位置来截取会出现Emoji截为2半
//超出最大长度的那个字符序列(Emoji算一个字符序列)的range
NSString * temText = @"";
NSString * checker = [NSString stringWithFormat:@"%C", 8198]; // 英文输入时的字符集
if ([[(UITextView *)Textfield text] rangeOfString:checker].length) {
temText = [[(UITextView *)Textfield text] stringByReplacingOccurrencesOfString:checker withString:@""];
}else{
temText = [(UITextView *)Textfield text];
}
NSRange range = [temText rangeOfComposedCharacterSequenceAtIndex:limitMaxWord];
((UITextView *)Textfield).text = [temText substringToIndex:range.location];
}
}
}