// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)color.CGColor
range:NSMakeRange(location, length)];
}
// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize*2,
NULL))
range:NSMakeRange(location, length)];
}
// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber numberWithInt:style]
range:NSMakeRange(location, length)];
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)color.CGColor
range:NSMakeRange(location, length)];
}
// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize*2,
NULL))
range:NSMakeRange(location, length)];
}
// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
if (location < 0||location>self.text.length-1||length+location>self.text.length) {
return;
}
[_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber numberWithInt:style]
range:NSMakeRange(location, length)];
}
//其他方法
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
attrLabel.attributedText = str;