//@ textView-Delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"@"]) {//回调@群成员
FeedUserListVC* vc = [[FeedUserListVC alloc]init];
vc.alterUserListBlock = ^(KLFeedNoteSqlModel*model){
_textview.text = [NSString stringWithFormat:@"%@@%@ ",_textview.text,model.nick_name];
[self setAlterAttributed];
};
vc.becomeFirstResponderBlock = ^(){
[_textview becomeFirstResponder];
};
vc.aterFlagForcancelBlock = ^(){
_textview.text = [NSString stringWithFormat:@"%@ @",_textview.text];
[self setAlterAttributed];
};
[self.navigationController presentViewController:[[UINavigationController alloc]initWithRootViewController:vc] animated:YES completion:nil];
}else{
//这里有个issue,要是不稍等一下,效果就很诡异,比如说在@字符后面刚输入第一个字:“好”字,参数text= “好”,但是断点来到这里_textview.text并没有得到这个字,所以在取range的时候会不准,但是多输入几个字又正常,所以姑且这样解决
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setAlterAttributed];
});
}
return YES;
}
//设置属性
-(void)setAlterAttributed{
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc]initWithString:_textview.text];
NSArray* alterArray = [self getRangesForUserHandles:_textview.text];
for (NSMutableDictionary* dicTemp in alterArray) {
[attributedString addAttribute:NSForegroundColorAttributeName value:[JJTool colorWithHexValue:0x5f6c80] range: [[dicTemp objectForKey:KILabelRangeKey] rangeValue]];
}
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range: NSMakeRange(0, attributedString.length)];
_textview.attributedText = attributedString;
}
//正则过滤@字段
- (NSArray *)getRangesForUserHandles:(NSString *)text
{
NSMutableArray *rangesForUserHandles = [[NSMutableArray alloc] init];
// Setup a regular expression for user handles and hashtags
static NSRegularExpression *regex = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSError *error = nil;
regex = [[NSRegularExpression alloc] initWithPattern:@"@[\u4e00-\u9fa5_a-zA-Z0-9-]+" options:0 error:&error];
// regex = [[NSRegularExpression alloc] initWithPattern:@"(?<!\\w)@([\\w\\_-]+)?" options:0 error:&error];
});
// Run the expression and get matches
NSArray *matches = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
// Add all our ranges to the result
for (NSTextCheckingResult *match in matches)
{
NSRange matchRange = [match range];
NSString *matchString = [text substringWithRange:matchRange];
// if (![self ignoreMatch:matchString])
// {
[rangesForUserHandles addObject:@{KILabelLinkTypeKey : @(KILinkTypeUserHandle),
KILabelRangeKey : [NSValue valueWithRange:matchRange],
KILabelLinkKey : matchString
}];
// }
}
return rangesForUserHandles;
}
//为了兼容系统键盘拼音输入故改之,也是奇葩了
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"@"]) {//回调@
FeedUserListVC* vc = [[FeedUserListVC alloc]init];
vc.alterUserListBlock = ^(KLFeedNoteSqlModel*model){
NSMutableString *targerStr = [[NSMutableString alloc] initWithString:_textview.text ];
[targerStr insertString:[NSString stringWithFormat:@"@%@ ",model.nick_name] atIndex:range.location];
_textview.text = targerStr;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setAlterAttributed];
_textview.selectedRange = NSMakeRange(range.location + [NSString stringWithFormat:@"@%@ ",model.nick_name].length, 0);
});
};
vc.aterFlagForcancelBlock = ^(){
NSMutableString *alterStr = [[NSMutableString alloc] initWithString:_textview.text ];
NSString* str = [NSString stringWithFormat:@" @"];
[alterStr insertString:str atIndex:range.location];
_textview.text = alterStr;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setAlterAttributed];
_textview.selectedRange = NSMakeRange(range.location + str.length, 0);
});
};
vc.becomeFirstResponderBlock = ^(){
[_textview becomeFirstResponder];
};
[self.navigationController presentViewController:[[UINavigationController alloc]initWithRootViewController:vc] animated:YES completion:nil];
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([text isEqualToString:@"➋"] || [text isEqualToString:@"➌"]||[text isEqualToString:@"➍"] || [text isEqualToString:@"➎"]||[text isEqualToString:@"➏"] || [text isEqualToString:@"➐"]||[text isEqualToString:@"➑"] || [text isEqualToString:@"➒"]) {
//系统九宫格不处理
}else{
if ([[[UITextInputMode currentInputMode] primaryLanguage] isEqualToString:@"zh-Hans"]) {
NSString* str = @"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
if ([str rangeOfString:text].location != NSNotFound) {
//系统全键盘不处理
}else{
[self setAlterAttributed];
_textview.selectedRange = NSMakeRange(range.location + text.length, 0);
}
}else{
[self setAlterAttributed];
_textview.selectedRange = NSMakeRange(range.location + text.length, 0);
}
}
});
}
return YES;
}