CustomLable

#import



@interface CustomerLable : UILabel {
    NSMutableAttributedString *mutaString;
}

@property (nonatomic,retain) NSMutableAttributedString *mutaString;


- (NSAttributedString *)illuminatedString:(NSString *)text font:(UIFont *)AtFont;

@end


//
//  CustomerLable.m
//  NSAttributeStr
//
//  Created by wgd on 12-4-23.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "CustomerLable.h"




@implementation CustomerLable
@synthesize mutaString;

- (NSAttributedString *)illuminatedString:(NSString *)text

                                     font:(UIFont *)AtFont{

    mutaString = [[[NSMutableAttributedString alloc] initWithString:@"adsfasfdadfadfas"] autorelease];
    if(!mutaString)
    {
        self.mutaString = [[NSMutableAttributedString alloc] init];
        [mutaString release];
    }

    
    
    CTFontRef  font_hello = CTFontCreateWithName((CFStringRef)@"Helvetica",16,NULL);
    
    CTFontRef  font_world = CTFontCreateWithName((CFStringRef)@"GillSans",20,NULL);
    
    [self.mutaString addAttribute:kCTFontAttributeName value:font_hello  range:NSMakeRange(0, 3)];
    
    [self.mutaString addAttribute:kCTFontAttributeName value:font_world  range:NSMakeRange(0, 3)];
    
    [self.mutaString addAttribute:(NSString *)(kCTForegroundColorAttributeName)
                    value:(id)[UIColor redColor].CGColor
                    range:NSMakeRange(0, 3)];  //放用户名的长度+1
    [self.mutaString addAttribute:(NSString *)(kCTForegroundColorAttributeName)
                    value:(id)[UIColor grayColor].CGColor
                    range:NSMakeRange(3, 2)];


    return mutaString;
    
}

//重绘Text

- (void)drawRect:(CGRect)rect

{
    
    //获取当前label的上下文以便于之后的绘画,这个是一个离屏。
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //压栈,压入图形状态栈中.每个图形上下文维护一个图形状态栈,并不是所有的当前绘画环境的图形状态的元素都被保存。图形状态中不考虑当前路径,所以不保存
    
    //保存现在得上下文图形状态。不管后续对context上绘制什么都不会影响真正得屏幕。
    
    CGContextSaveGState(context);
    
    //x,y轴方向移动
    
    CGContextTranslateCTM(context, 0.0, 0.0);
    
    //缩放x,y轴方向缩放,-1.0为反向1.0倍,座标系转换,沿x轴翻转180度
    
    // CGContextScaleCTM(context, 1, 100);
    
   // CGContextScaleCTM(context, 1, -1);    
    CGContextScaleCTM(context, 1, -1);    

    CGContextTranslateCTM(context, 0.0, - ceill(self.bounds.size.height) + 8);
    
 
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)mutaString);
    //设置文字绘画的起点座标。
    
    CGContextSetTextPosition(context, 0.0, 0.0);
    
    //在离屏上绘制line
    
    CTLineDraw(line, context);
    
    //将离屏上得内容覆盖到屏幕。此处得做法很像windows绘制中的双缓冲。
    
    CGContextRestoreGState(context);
    
    CFRelease(line);
    
    
}

-(void)dealloc
{
    [mutaString release];
    [super dealloc];
}

@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章