CTFontDrawGlyphs 与 CGContextShowGlyphsAtPoint 详解 CGGlyph详解

//CTFontDrawGlyphs CGContextShowGlyphsAtPoint 详解

//CGGlyph CGFontIndex 只是fontindex并不是glyph的数据存储地址


    //此方法获取fontSize时会crash

    self.font = [UIFont systemFontOfSize:18];

    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);

    CTFontDescriptorRef ctFontDesRef = CTFontCopyFontDescriptor(font);

    CGFontRef cgFont = CTFontCopyGraphicsFont(font,&ctFontDesRef );

    CFNumberRef pointSizeRef = (CFNumberRef)CTFontDescriptorCopyAttribute(ctFontDesRef,kCTFontSizeAttribute);

    CGFloat fontSize;

    CFNumberGetValue(pointSizeRef, kCFNumberCGFloatType,&fontSize);

    CGContextSetFontSize(context, fontSize);

    //此方法获取fontSize时不会crash

    //font = CTFontCreateWithName(CFSTR("ArialMT"), 20.0, NULL);

    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);

    CTFontDescriptorRef ctFontDesRef = CTFontCopyFontDescriptor(font);

    CGFontRef cgFont = CTFontCopyGraphicsFont(font,&ctFontDesRef );

    CFNumberRef pointSizeRef = (CFNumberRef)CTFontDescriptorCopyAttribute(ctFontDesRef,kCTFontSizeAttribute);

    CGFloat fontSize;

    CFNumberGetValue(pointSizeRef, kCFNumberCGFloatType,&fontSize);

    CGContextSetFontSize(context, fontSize);


- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //CTFontDrawGlyphs 与 CGContextShowGlyphsAtPoint 详解
    
    NSUInteger length = self.text.length;
    unichar chars[length];
    CGGlyph glyphs[length];
    
    self.font = [UIFont systemFontOfSize:18];
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    
    [self.text getCharacters:chars range:NSMakeRange(0, length)];
    CTFontGetGlyphsForCharacters(font, chars, glyphs, length);
    
//以下两种方法都可以绘制glyphs但是由于使用不用的坐标原点
//coretext的坐标原点在左下角
//coregraphic的坐标原点在左上角
//因此使用coretext和coregraphic绘制glyphs时坐标矩阵的处理是不同的
//另外使用CTFontDrawGlyphs函数绘制glyphs时需要保证CGPoint数组要和glyphs的数据个数保持一致
#if 1 
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextStrokePath(context);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    
    const CGPoint points[] = {
        20,30,
        40,30,
        60,30,

        80,30,
    };
    const CGPoint *  pointsPoint = points;
    CTFontDrawGlyphs(font, glyphs, pointsPoint, 4, context);
#else
    CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(context, textTransform);
    
    CGContextStrokePath(context);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    
    CTFontDescriptorRef ctFontDesRef = CTFontCopyFontDescriptor(font);
    CGFontRef cgFont = CTFontCopyGraphicsFont(font,&ctFontDesRef );
    CGContextSetFontSize(context, 20);
    CGContextSetFont(context, cgFont);
    //使用CGContextShowGlyphsAtPoint画glyphs时必须要设置字体和字体大小
    CGContextShowGlyphsAtPoint(context, 0, 20, glyphs, 1);
#endif
    
    CFRelease(font);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值