penn-wang
一位老和尚,他身边聚拢着一帮虔诚的弟子。这一天,他嘱咐弟子每人去南山打一担柴回来。弟子们匆匆行至离山不远的河边,人人目瞪口呆。只见洪水从山上奔泻而下,无论如何也休想渡河打柴了。无功而返,弟子们都有些垂头丧气。唯独一个小和尚与师傅坦然相对。师傅问其故,小和尚从怀中掏出一个苹果,递给师傅说,过不了河,打不了柴,见河边有棵苹果树,我就顺手把树上唯一的一个苹果摘来了。后来,这位小和尚成了师傅的衣钵传人。

上图:

不要怀疑,这是一个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对象

posted on 2013-01-15 17:22  penn-wang  阅读(511)  评论(0编辑  收藏  举报