iOS-改变字符串中指定的字符颜色和大小

2017-07-25  本文已影响264人  学_iOS
self.secondLabel.text = @"0123456789";

NSDictionary *dict = [self titleTextAttributesWithTitleColor:[UIColor redColor] WithTiteleFont:[UIFont systemFontOfSize:23]];

NSMutableAttributedString * tempString = [[NSMutableAttributedString alloc] initWithString: self.secondLabel.text];

 //指定要改变的字符的开始位置和长度     
[tempString addAttributes:dict range:NSMakeRange(6, 4)];

self.secondLabel.attributedText = tempString;
/*
 * 带有字体颜色和字体大小的字典
 */
- (NSMutableDictionary *)titleTextAttributesWithTitleColor:(UIColor *)titleColor WithTiteleFont:(UIFont *)titleFont{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    dict[NSForegroundColorAttributeName] = titleColor;

    dict[NSFontAttributeName] = titleFont;

    return dict;
}
上一篇下一篇

猜你喜欢

热点阅读