上图:
不要怀疑,这是一个label。
代码 ColorsLabel.h
@interface ColorsLabel : UILabel { NSMutableDictionary *_attributedDic; NSMutableAttributedString *_attributedText; } @property (nonatomic, copy) NSMutableDictionary *attributedDic; @end
ColorsLabel.m
#import <CoreText/CoreText.h> #import "ColorsLabel.h" #import "BoxToolUtility.h" @implementation ColorsLabel @synthesize attributedDic = _attributedDic; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void) dealloc { [_attributedDic release]; [_attributedText release]; [super dealloc]; } #pragma -mark -property - (void)setAttributedDic:(NSMutableDictionary *)attributedDic { _attributedDic = [attributedDic copy]; _attributedText = [[NSMutableAttributedString alloc] initWithString:self.text]; UIColor *colorAll = [BoxToolUtility colorWithHexString:@"#361f04"]; // UIColor *colorSpecial = [BoxToolUtility colorWithHexString:@"#ce3800"]; [_attributedText addAttribute:(NSString*)(kCTForegroundColorAttributeName) value:(id)[colorAll CGColor] range:NSMakeRange(0, self.text.length)]; for (NSString *key in [_attributedDic allKeys]) { [_attributedText addAttribute:(NSString*)(kCTForegroundColorAttributeName) value:(id)[[_attributedDic objectForKey:key]CGColor] range:[BoxToolUtility rangeFromString:key]]; } CTFontRef font_world = CTFontCreateWithName((CFStringRef)@"Helvetica-Bold",18,NULL); [_attributedText addAttribute: (NSString*)(kCTFontAttributeName) value:(id)font_world range:NSMakeRange(0,self.text.length)]; CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;//换行模式 CTTextAlignment alignment = kCTLeftTextAlignment;//对齐方式 float lineSpacing = 1.0;//行间距 CTParagraphStyleSetting paraStyles[3] = { {.spec = kCTParagraphStyleSpecifierLineBreakMode,.valueSize = sizeof(CTLineBreakMode), .value = (const void*)&lineBreakMode}, {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = (const void*)&alignment}, {.spec = kCTParagraphStyleSpecifierLineSpacing,.valueSize = sizeof(CGFloat), .value = (const void*)&lineSpacing}, }; CTParagraphStyleRef style = CTParagraphStyleCreate(paraStyles,3); [_attributedText addAttribute:(NSString*)(kCTParagraphStyleAttributeName) value:(id)style range:NSMakeRange(0,[self.text length])]; CFRelease(style); // [attributedText addAttribute:(NSString*)(kCTForegroundColorAttributeName) value:(id)[[UIColor redColor]CGColor] range:NSMakeRange(6,5)]; } - (void) drawTextInRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context,CGAffineTransformIdentity);//重置 CGContextTranslateCTM(context,0,self.bounds.size.height); //y轴高度 CGContextScaleCTM(context,1.0,-1.0);//y轴翻转 它丫的,coortext是反的。 CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attributedText); CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path,NULL,self.bounds); CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),path,NULL); CGContextSetTextPosition(context,0,0); CTFrameDraw(frame,context); CFRelease(framesetter); CFRelease(frame); } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
使用的时候,只需要把ColorsLabel的字典设置。 字典值为uicolor,key为range 既设置的range的颜色为color。
ColorsLabel *colors = [[ColorsLabel alloc] initWithFrame:CGRectMake(50, 160, 160, 50)]; colors.text = @"距目的地还有20站,约45分钟"; colors.font = [UIFont systemFontOfSize:18.0]; colors.backgroundColor = [UIColor clearColor]; NSMutableDictionary *d = [[NSMutableDictionary alloc] init]; [d setObject:[BoxToolUtility colorWithHexString:@"#ce3800"] forKey:[BoxToolUtility stringFromRange:NSMakeRange(6, 2)]]; [d setObject:[BoxToolUtility colorWithHexString:@"#ce3800"] forKey:[BoxToolUtility stringFromRange:NSMakeRange(11, 2)]]; colors.attributedDic = d; [d release]; [self addSubview:colors];
[BoxToolUtility colorWithHexString:@"#ce3800"]//返回 uicolor对象