在 UITableViewCell 中处理 NSRange

Handling NSRange In UITableViewCell

我在使用 NSRange 时遇到了一些问题。我有一个 CommentViewController 可以工作,但我正在尝试使用 tapGesture 并在 @ 之后立即更改文本的颜色,就像推特上提到的那样.出于某种原因,在某些单元格中,所有文本都会改变颜色,而不仅仅是提及。这是代码:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:message[@"text"]];

NSArray *words=[message[@"text"] componentsSeparatedByString:@""];

for (NSString *word in words) {
     if ([word hasPrefix:@"@"]) {
            NSRange range=[message[@"text"] rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
     }
}
[cell.bodyLabel setAttributedText:string];

我哪里做错了,如何在彩色部分添加手势?

替换

   NSArray *words=[message[@"text"] componentsSeparatedByString:@""];

    NSArray *words=[message[@"text"] componentsSeparatedByString:@" "];

您应该用白色 space 分隔组件,因为您分隔单词而不是字母!!

你试过这个吗

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:message[@"text"]];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@[A-Za-z0-9]*)" options:kNilOptions error:nil];

NSRange range = NSMakeRange(0,message[@"text"].length);

[regex enumerateMatchesInString:message[@"text"] options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

    NSRange subStringRange = [result rangeAtIndex:1];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
}];

cell.bodyLabel.attributedText = attributedString;
NSString *message = @"hello@gmail.com";

NSRange initialRange = [message rangeOfString:@"@"];
NSRange endRange = [message rangeOfString:@"."];
NSUInteger length = endRange.location - initialRange.location;
NSRange markupRange = NSMakeRange(initialRange.location, length);
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc]initWithString:message];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:markupRange];

[cell.bodyLabel setAttributedText:attString];

这相当粗糙,我建议添加一些条件检查以确保您不会因 .在 @ 之前,但它确实有效。

请试试这个

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:message[@"text"]];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@[A-Za-z0-9]*)" options:kNilOptions error:nil];
NSArray *a = [message[@"text"]componentsSeparatedByString:@" "];

for (NSString *str in a) 
{
    NSRange range = NSMakeRange(0,str.length);

    [regex enumerateMatchesInString:str options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

        NSRange subStringRange = [result rangeAtIndex:1];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
    }];
    label.attributedText = attributedString;
}

我想通了。在大多数情况下,我认为这是正则表达式的问题!这是我的最终代码:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:strFirst];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:(NSRange){0, strFirst.length}];
UIColor *mentionColor = [UIColor hx_colorWithHexRGBAString:@"2cb8f6"];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[#@](\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:strFirst options:0 range:NSMakeRange(0, strFirst.length)];
for (NSTextCheckingResult *match in matches) {
    NSRange wordRange = [match rangeAtIndex:0];
  //  NSString *word = [strFirst substringWithRange:wordRange];
    [attributedString addAttribute:NSForegroundColorAttributeName
                             value: mentionColor
                             range:wordRange];
}
cell.bodyLabel.attributedText = attributedString;