【问题标题】:iPhone - UITableView cell label color changeiPhone - UITableView 单元格标签颜色更改
【发布时间】:2015-04-16 18:48:47
【问题描述】:

我有基于搜索文本的 UITableView 数据。如何在所有数据单元格或整个 UITableView 中突出显示 UITableViewCell 标签中的文本?

这样的例子,如果我用“Ball”搜索,UITableView 加载“Red”文本包含需要的数据现在我需要关注所有包含“Ball”/“ball”文本的标签。

“球”是一个简单的搜索文本。

怎么样?我正在使用 iOS v 8.2 和 Objective-c

【问题讨论】:

  • 以下答案似乎与您的要求不符。人们专注于使标签更改颜色,而您正在询问如何根据搜索文本更改颜色。再次,请在 cellForRowAtIndexPath 方法中发布您的代码。

标签: ios objective-c iphone uitableview


【解决方案1】:

你需要使用NSAttributedString

//This needs to go into cellForRowAtIndexPath:
//Set title label
//StrTerm is result, and seachText is text searched for.
//Adjust the fonts according to your needs

NSMutableAttributedString *mattrStr = [[NSMutableAttributedString alloc]initWithString:[strTerm capitalizedString]];

[mattrStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, strTerm.length)];
[mattrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:16.0] range:NSMakeRange(0, strTerm.length)];

NSRange range = [[strTerm lowercaseString] rangeOfString:[searchText lowercaseString]];

[mattrStr setAttributes:@{
                          NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0],
                          NSForegroundColorAttributeName: [UIColor whiteColor]
                          } range:range];

cell.textLabel.attributedText = mattrStr;

【讨论】:

  • 它在自定义 UITableViewCell 中不起作用,知道吗?
  • 您是否在自定义 UITableViewCell 上使用了自己的标签?
  • 不完全是我设计了一个包含一些内容(图像、评级、标签...)的详细视图。我想改变那个标签文本的颜色,就是这样。
【解决方案2】:

您可以使用以下代码突出显示标签的文本颜色。 当您仅在 tableview 中加载搜索内容时在下面应用..

cell.textLabel.highlightedTextColor = [UIColor redColor];

//(OR) 如果您使用的是自定义单元格,则使用以下代码...

cell.lblRef.highlightedTextColor = [UIColor redColor];

希望对您有所帮助...!

【讨论】:

    【解决方案3】:

    使用此代码..

    NSString *initial = @"Lorem ipsum and dolor sit er elit lamet, consectetaur cillium adipisicing pecu, and sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation and ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse and cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
            NSString *text = [initial stringByReplacingOccurrencesOfString:@"and" withString:@"ex"]; //check for "and" replacing with "or"
    
            NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
    
            NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(ex)" options:kNilOptions error:nil];
    
    
            NSRange range = NSMakeRange(0,text.length);
    
            [regex enumerateMatchesInString:text options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    
                NSRange subStringRange = [result rangeAtIndex:1];
                [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
            }];
            [_lableText setAttributedText:mutableAttributedString];
    

    NSMutableAttributedString_Class

    【讨论】:

      【解决方案4】:

      试试这个。 TTTAttributedLabel

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 2019-08-03
        • 2018-12-25
        • 1970-01-01
        • 1970-01-01
        • 2020-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多