注意:
1.使用前请导入
#import<CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
这两个类库。
2.给label设置属性时一定要先给label.text赋值,然后再添加属性。
3.最终显示label前,要重新加载一下 setAttributedText。
[self.lyricLab setAttributedText:self.lyricLab.attString];
.h
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
@interface AttributedLabel : UILabel{
NSMutableAttributedString *_attString;
}
@property (nonatomic,retain)NSMutableAttributedString *attString;
// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length;
// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length;
// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length;
- (void)replaceColor:(UIColor *)color fromIndex:(NSInteger)location lenth:(NSInteger)lenth;
@end
.m
#import "AttributedLabel.h"
@interface AttributedLabel(){
}
@end
@implementation AttributedLabel
@synthesize attString = _attString;
- (void)dealloc{
[_attString release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.text !=nil) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, 0.0);//move
CGContextScaleCTM(context, 1.0, -1.0);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attString);
CGMutablePathRef pathRef = CGPathCreateMutable();
CGPathAddRect(pathRef,NULL , CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height));//const CGAffineTransform *m
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), pathRef,NULL );//CFDictionaryRef frameAttributes
CGContextTranslateCTM(context, 0, -self.bounds.size.height);
CGContextSetTextPosition(context, 0, 0);
CTFrameDraw(frame, context);
CGContextRestoreGState(context);
CGPathRelease(pathRef);
CFRelease(framesetter);
UIGraphicsPushContext(context);
}
}
- (void)setText:(NSString *)text{
[super setText:text];
if (text == nil) {
self.attString = nil;
}else{
self.attString = [[[NSMutableAttributedString alloc] initWithString:text] autorelease];
}
self.lineBreakMode = NSLineBreakByWordWrapping;
self.numberOfLines = 0;
}
// 设置某段字的颜色
- (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 removeAttribute:(NSString *)kCTForegroundColorAttributeName range:NSMakeRange(location, length)];
[_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)CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize,
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)];
}
如果有问题,可以联系我qq:484096839