自学1

- (void)drawRect:(CGRect)rect{

    [super drawRect:rect];
    
    
    
    
    //获取当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //坐标点转换(低层的左下角是(0,0),UIKit是左上角为(0,0))
    CGContextSetTextMatrix(context, CGAffineTransformIdentity)
    ;
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    

    
    //创建绘制区域
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, self.bounds);
    
    //文字
    NSMutableAttributedString * attristr = [[NSMutableAttributedString alloc] initWithString:@"This is a test 文字字符"];
    
    
    //设置字体大小属性
    //    CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 30, NULL);
    //    [attristr addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, 4)];
    
    
    //设置斜体字
    //    CTFontRef font2 = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:25].fontName, 30, NULL);
    //
    //    [attristr addAttribute:(id)kCTFontAttributeName value:(__bridge id)font2 range:NSMakeRange(0, 4)];
    
    
    //下划线
    //    [attristr addAttribute:(id)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:NSMakeRange(0, 4)];
    //    [attristr addAttributes:@{(id)kCTUnderlineStyleAttributeName:[NSNumber numberWithInt:kCTUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor blueColor]} range:NSMakeRange(0, 4)];
    
    //下划线颜色
    //    [attristr addAttributes:@{(id)kCTUnderlineColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 4)];
    
    
    //设置字体间隔
    //    long number = 9;
    //    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
    //    [attristr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(10, 4)];
    
    //设置字体颜色
    //    [attristr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
    
    //设置空心字
    long number = 3;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
    [attristr addAttribute:(id)kCTStrokeWidthAttributeName value:(__bridge id)num range:NSMakeRange(0, 4)];
    
    //设置空心颜色
    [attristr addAttribute:NSStrokeColorAttributeName value:(id)[UIColor blueColor] range:NSMakeRange(0, 4)];
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attristr);
    
    
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attristr length]), path, NULL);
    
    CTFrameDraw(frame, context);
    
    
    CFRelease(frame);
    CFRelease(path);
    CFRelease(framesetter);
    
    
    
}

你可能感兴趣的:(自学1)