iOS - coreText简单使用

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    //获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //坐标转换
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1);
    
    
    //绘制区域
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(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);
    
    
    CFRelease(frame);
    CFRelease(path);
    CFRelease(framesetter);
    
    
}

你可能感兴趣的:(iOS - coreText简单使用)