繁体   English   中英

在UITableViewCell中处理NSRange

[英]Handling NSRange In UITableViewCell

我在NSRange遇到了一些问题。 我有一个CommentViewController可以正常工作,但是我想拥有一个tapGesture并在@之后更改文本的颜色,就像Twitter上提到的那样。 由于某些原因,在某些单元格中,所有文本都会更改颜色,而不仅仅是提及。 这是代码:

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:@" "];

分隔单词而不是字母时,应使用空格分隔组成部分!!

你有试过吗

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM