基于CoreText的基础排版引擎

```

#import“CoreText/CoreText.h"

-(void)drawRect:(CGRect)rect

{

[super drawRect:rect];

//步骤1 得到当前绘制画布的上下文

CGContextRef  context = UIGraphaicsGetCurrentContext();

//步骤二

CGContextSetTextMatrix(context,CGAffineTrasformIdentity);

CGContextTranslateCTM(context,0,self.bounds.size.height);

CGConTextScaleCTM(context,1.0,-1.0);

//将坐标系上下翻转 对于底层的绘制引擎来说屏幕的左下角是(0,0)坐标,而对于上层的UIKit来说左上角是(0,0);

//步骤三创建绘制的区域,CoreText 本身支持各种文字排版的区域

CGMutablePathRef  path = CGPathCreateMutable();

CGPathAddRect(path,NULL,self.bounds);

//步骤四

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"你好世界"];

CTFramesetterRef   framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);

CTFrameRef   frame  = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,[attString length]),path,NULL);

//步骤五

CTFrameDraw(frame,context);

//步骤六

CFRease(frame);

CFRease(path);

CFRease(framesetter);

}

}

```

UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);

    CGContextRef context = UIGraphicsGetCurrentContext();


    CGContextAddEllipseInRect(context, CGRectZero);

    //设置透明色

    CGContextSetBlendMode(context, kCGBlendModeClear);

    CGContextDrawPath(context, kCGPathFill);

    UIImage*anewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIBezierPath *path;

    CGContextAddPath(context, path.CGPath);

你可能感兴趣的:(基于CoreText的基础排版引擎)