2013-10-11 56 views
20

我有一个字符串,其中最后一个字总是与其他字在整个字符串中的颜色不同,例如:如果字符串颜色是黑色,则语句中的最后一个字会是红色的。我使用NSAttributed串吧,如下:使用NSAttributed字符串更改字符串的颜色和字体

NSDictionary *attrDict = [NSDictionary dictionaryWithObject:[UIFont fontWithName:Arial size:16.0] forKey:NSFontAttributeName]; 
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:@"This is a" attributes: attrDict]; 

我将作出类似NSAttributedString的最后一个字,将与第一个字符串的修订。

问题:我想给字符串添加颜色和字体。我已经在使用字典处理字体。但是,我可以在同一个字典中添加颜色处理程序/代码,或者在NSAttributedString上使用“addAttribute:”是添加颜色或任何其他属性的唯一方法吗?

回答

54

颜色添加到相同的字典的字体:

NSDictionary *attrDict = @{ 
    NSFontAttributeName : [UIFont fontWithName:Arial size:16.0], 
    NSForegroundColorAttributeName : [UIColor redColor] 
};