实现效果:1.@好友 文本颜色,字体改变
2.@好友整体删除
代码详解:
1.找到所有"@xxx "格式的文本,@好友后加空格形成一种格式
#define kATFormat @"@%@ "
#define kATRegular @"@[\\u4e00-\\u9fa5\\w\\-\\_]+ "
- (NSArray *)findAllAt
{
// 找到文本中所有的@
NSString *string = self.sayTextView.text;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:kATRegular options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [regex matchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, [string length])];
return matches;
}
2.textview文本改变除@好友 之外别的字体设置
- (void)textViewDidChange:(UITextView *)textView{
UITextRange *selectedRange = textView.markedTextRange;
NSString *newText = [textView textInRange:selectedRange];
if (newText.length < 1)
{
// 高亮输入框中的@
UITextView *textView = self.sayTextView;
NSRange range = textView.selectedRange;
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:textView.text];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.string.length)];
[string addAttribute:NSFontAttributeName value:KFont(14) range:NSMakeRange(0, string.string.length)];
NSArray *matches = [self findAllAt];
for (NSTextCheckingResult *match in matches)
{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(match.range.location, match.range.length - 1)];
[string addAttribute:NSFontAttributeName value:KFont(14) range:NSMakeRange(match.range.location, match.range.length - 1)];
}
textView.attributedText = string;
textView.selectedRange = range;
}
}
3.光标不能落在@词中间
- (void)textViewDidChangeSelection:(UITextView *)textView{
// 光标不能点落在@词中间
NSRange range = textView.selectedRange;
if (range.length > 0)
{
// 选择文本时可以
return;
}
NSArray *matches = [self findAllAt];
for (NSTextCheckingResult *match in matches)
{
NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
if (NSLocationInRange(range.location, newRange))
{
textView.selectedRange = NSMakeRange(match.range.location + match.range.length, 0);
break;
}
}
}
4.整体删除@好友
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if ([text isEqualToString:@""])
{
NSRange selectRange = textView.selectedRange;
if (selectRange.length > 0)
{
//用户长按选择文本时不处理
return YES;
}
// 判断删除的是一个@中间的字符就整体删除
NSMutableString *string = [NSMutableString stringWithString:textView.text];
NSArray *matches = [self findAllAt];
BOOL inAt = NO;
NSInteger index = range.location;
for (NSTextCheckingResult *match in matches)
{
NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
if (NSLocationInRange(range.location, newRange))
{
NSString *matchStr = [string substringWithRange:NSMakeRange(match.range.location, match.range.length - 1)];
[_atDic removeObjectForKey:matchStr];
inAt = YES;
index = match.range.location;
[string replaceCharactersInRange:match.range withString:@""];
break;
}
}
if (inAt)
{
textView.text = string;
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attributeString.string.length)];
[attributeString addAttribute:NSFontAttributeName value:KFont(14) range:NSMakeRange(0, attributeString.string.length)];
NSArray *matches = [self findAllAt];
for (NSTextCheckingResult *match in matches)
{
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(match.range.location, match.range.length - 1)];
[attributeString addAttribute:NSFontAttributeName value:KFont(14) range:NSMakeRange(match.range.location, match.range.length - 1)];
}
textView.attributedText = attributeString;
textView.selectedRange = NSMakeRange(index, 0);
return NO;
}
}
//判断是回车键就发送出去
if ([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
5.获取@好友的好友Id
NSArray *matches = [self findAllAt];
NSMutableArray *strArr = [NSMutableArray arrayWithCapacity:0];
for (NSTextCheckingResult *match in matches)
{
NSString *matchStr = [self.sayTextView.text substringWithRange:NSMakeRange(match.range.location, match.range.length - 1)];
[strArr addObject:[_atDic objectForKey:matchStr]];
}
6.添加@好友的好友Id
NSString *insertString = [NSString stringWithFormat:kATFormat,atUserName];
NSString *keyString = [NSString stringWithFormat:@"@%@",atUserName];
[_atDic setValue:atUserId forKey:keyString];