0

我有一个字符串...。Share Pictures我想以Share不同的颜色显示。我曾经NSMutableAttributedString更改字符串那部分的颜色。但是当我cell.textLabel.text使用以下字符串设置它时它不起作用。任何其他方式做这个?

这种方式是行不通的。

NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:@"Share pictures "];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
NSString *tempStr3 = @"with your friends.";
NSString *finalString3 = [NSString stringWithFormat:@"%@%@" , string3, tempStr3];
[menuTextArray addObject:finalString3];

并在表视图数据源方法中。

 cell.textLabel.text = [menuTextArray objectAtIndex:indexPath.row];
4

3 回答 3

2

您只需要添加NSMutableAttributedString到您的menuTextArray

NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc]initWithString:@"Share pictures with your friends"];
[yourString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
[menuTextArray addObject:yourString];

然后设置attributedText

cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
于 2013-12-26T09:45:03.810 回答
1

使用attributedText代替text

cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
于 2013-12-26T09:38:36.760 回答
0

希望这段代码对你有帮助......

    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:@"Share pictures with your friends"];
    [string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 5)];
    [menuTextArray addObject:string3];
    [cell.textLabel setAttributedText:string3];
于 2013-12-26T09:57:09.800 回答