-1

我无法使用模拟器 5.0 在 xcod4.2 中编写以下代码告诉我替代解决方案

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initwithString:@"This is Green and Red"];
NSDictionary *redatt=@{NSForeGroundColorAttributeName:[UIColor redColor]};
NSDictionary *greenatt=@{NSForeGroundColorAttributeName:[UIColor greenColor]};

[str setAttribute:greenatt range:NSMakeRange(9,5)];
[str setAttribute:redatt range:NSMakeRange(19,3)];

lbl.attributedText=str;
4

2 回答 2

1

您需要导入CoreText Framework,然后执行它。尝试使用kCTForegroundColorAttributeName。有关更多信息,请参阅此示例

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initwithString:@"This is Green and Red"];


[str addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:NSMakeRange(9,5)];
[str addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor greenColor] CGColor] range:NSMakeRange(19,3)];

lbl.attributedText=str;
于 2013-10-21T07:17:39.423 回答
0

试试这个,试过效果很好:

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:@"This is Green and Red"] ;

    NSDictionary *redatt=@{NSForegroundColorAttributeName:[NSColor redColor]};
    NSDictionary *greenatt=@{NSForegroundColorAttributeName:[NSColor greenColor]};

    [str addAttributes:redatt  range:NSMakeRange(9,5)];
    [str addAttributes:greenatt  range:NSMakeRange(1,4)];

    [lbl.textStorage setAttributedString:str];
于 2013-10-21T07:54:54.930 回答