我有基于搜索文本的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/