美文网首页iOS
CoreText学习笔记(1)

CoreText学习笔记(1)

作者: Foryoujust | 来源:发表于2015-01-28 09:48 被阅读260次

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

    CGContextScaleCTM(context, 1.0, -1.0);

    在iOS中使用CoreText需要转换

    绘制一段文字

    1、获取需要绘制的文字

    CFStringRef textString = (__bridge CFStringRef)@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    2、创建属性字符串

    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);

    3、将绘制位子复制到属性字符串中

    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0),textString);

    4、设置属性,例如颜色

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat components[] = { 1.0, 0.0, 0.0, 0.8 };

    CGColorRef red = CGColorCreate(rgbColorSpace, components);

    CGColorSpaceRelease(rgbColorSpace);

    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 14),kCTForegroundColorAttributeName, red);

    5、根据属性字符串创建FrameSetter

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);

    CFRelease(attrString);

    6、创建绘制路径

    CGMutablePathRef path = CGPathCreateMutable();

    CGRect bounds = CGRectMake(10.0, 10.0, 190.0, 200.0);

    CGPathAddRect(path, NULL, bounds );

    7、根据路径和frameSetter创建Frame

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), path, NULL);

    8、在当前上下文中绘制Frame

    CTFrameDraw(frame, context);

    CFRelease(frame);

    CFRelease(path);

    CFRelease(framesetter);

    相关文章

      网友评论

        本文标题:CoreText学习笔记(1)

        本文链接:https://www.haomeiwen.com/subject/wlxlxttx.html