【问题标题】:Handling NSRange In UITableViewCell在 UITableViewCell 中处理 NSRange
【发布时间】:2016-07-11 04:33:40
【问题描述】:

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];

我做错了什么?如何在彩色部分添加手势?

【问题讨论】:

  • 你想改变文字颜色。我的意思是前:iiiiii@gmail,你可以改变 gmail 的颜色。
  • 我想更改“@gmail” - 是的!
  • NSArray *words=[message[@"text"] componentsSeparatedByString:@""];到 NSArray *words=[message[@"text"] componentsSeparatedByString:@" "];那里缺少空间
  • @HalesEnchanted 检查我的答案。
  • @HalesEnchanted ?

标签: ios objective-c uitableview nsstring nsrange


【解决方案1】:

你试过了吗

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;

【讨论】:

  • 感谢您的帮助。出于某种原因,例如,如果 strFirst 以 @gmail 开头,则整个文本仍然是红色的。如果我在它前面放一些文字,例如:'haha @gmail' - 它可以工作。会是什么?
  • 你能把整个输入让我理解吗?如果@ gmail 表示如果空间存在,那么您还希望gmail 带有红色吗?是你想要的吗?
  • 我和你一样有代码:) 问题是当 @ 开始句子时,它不起作用。这是一张照片:i.imgur.com/URTgMIJ.png
  • 我这边工作正常,你可以在这里查看i.stack.imgur.com/J5HNF.png。你有没有用我的代码替换整个代码。
  • 太奇怪了。难道是因为它是一个单元格/索引路径?
【解决方案2】:

替换

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

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

当你分隔单词而不是字母时,你应该用空格分隔组件!

【讨论】:

    【解决方案3】:
    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];
    

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

    【讨论】:

      【解决方案4】:

      请试试这个

      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;
      }
      

      【讨论】:

      • 这让一切都变红了!怎么会?
      【解决方案5】:

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

      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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 2015-09-30
        • 1970-01-01
        • 2010-09-25
        • 2014-09-25
        • 1970-01-01
        相关资源
        最近更新 更多