您的位置:首页 > 移动开发 > IOS开发

iOS 设置文本中指定某段文本的颜色 大小

2016-01-27 12:46 459 查看
NSString *money = @"300";

NSString *perStr = @"元/时";

NSString *text = [NSString stringWithFormat:@"%@%@",money,perStr];

if ([self.currentPriceLabel respondsToSelector:@selector(setAttributedText:)]) {

NSDictionary *attribs = @{

NSForegroundColorAttributeName:self.currentPriceLabel.textColor,

NSFontAttributeName:self.currentPriceLabel.font

};

NSMutableAttributedString *attributedText =

[[NSMutableAttributedString alloc] initWithString:text

attributes:attribs];

// Red text attributes

//#5773de

UIColor *aColor = RED_COLOR;

NSRange redTextRange =[text rangeOfString:money];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration

[attributedText setAttributes:@{NSForegroundColorAttributeName:aColor,NSFontAttributeName:[UIFontboldSystemFontOfSize:16]} range:redTextRange];

self.currentPriceLabel.attributedText = attributedText;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: