gpt4 book ai didi

ios - 一个 UITableViewCell 文本标签中的两种文本颜色

转载 作者:可可西里 更新时间:2023-11-01 06:18:28 26 4
gpt4 key购买 nike

我在 UITableViewCell 中有一个文本标签,由两个单词组成。

如何改变文字的颜色;把第一个词变成绿色,第二个词变成红色?

最佳答案

NSString *twoWords = @"Green Red";
NSArray *components = [twoWords componentsSeparatedByString:@" "];
NSRange greenRange = [twoWords rangeOfString:[components objectAtIndex:0]];
NSRange redRange = [twoWords rangeOfString:[components objectAtIndex:1]];

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:twoWords];

[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor greenColor]
range:greenRange];

[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor redColor]
range:greenRange];

[attrString endEditing];

然后您可以直接在 UILabel 上使用 attrString(> iOS 6,检查 Apple Documentation )。

关于ios - 一个 UITableViewCell 文本标签中的两种文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680018/

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