iOS中UILabel显示不同的样式的文字属性
– (void)updateTextLabelWith:(NSInteger)count withTotalPrice:(CGFloat)price
{
NSString *countString = [NSString stringWithFormat:@”%d”, count];
NSString *priceString = [NSString stringWithFormat:@”%.2f”, price];
NSString *tempString = [NSString stringWithFormat:@”您已点 %@ 个菜,共计 ¥%@”, countString, priceString];
NSInteger tempCount = 0;
tempCount += 4;
// 重新生成一个可变的字符串
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:tempString];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]
range:NSMakeRange(0, tempCount)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:18.0]
‘ range:NSMakeRange(0, tempCount)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f]
range:NSMakeRange(tempCount, countString.length)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0]
range:NSMakeRange(tempCount, countString.length)];
tempCount += countString.length + 1;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]
range:NSMakeRange(tempCount, 6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:18.0]
range:NSMakeRange(tempCount, 6)];
tempCount += 6;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f]
range:NSMakeRange(tempCount, 1)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0]
range:NSMakeRange(tempCount, 1)];
tempCount += 1;
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfb3a42 alpha:1.0f]
range:NSMakeRange(tempCount, priceString.length)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”HelveticaNeue” size:30.0]
range:NSMakeRange(tempCount, priceString.length)];
[self.textLabel setAttributedText:str];
[str release];
}