ios - iPhone-UITableView单元格标签颜色更改

标签 ios objective-c iphone uitableview

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

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

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

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

最佳答案

您需要使用 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;

关于ios - iPhone-UITableView单元格标签颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666952/

相关文章:

iphone - 计算 MKPolyline 路径的距离?

ios - Swift 泛型和协议(protocol)

ios - phonegap (cordova) 是否支持使用声音捕获视频?

ios - 如何处理 `Use of undeclared identifier ' UIDevice'`

iphone - NSArray 和 NSMutableArray 的区别

iphone - 多结局故事的数据结构

iphone - 我可以将图像转换为点网格吗?

ios - 移动和缩放反弹

objective-c - 区分对同一委托(delegate)方法的调用

ios - 使用 animationWithDuration 更改 UIButton 的标题没有延迟或动画