gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:53:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com