使用CoreText画文字的边框

Drawing outlined glyphs with CoreText

https://gist.github.com/randomsequence/5755157

#import "OutlineTextView.h"
#import <CoreText/CoreText.h>

@implementation OutlineTextView

- (void) commonInit {
    self.backgroundColor = [UIColor blackColor];
    self.opaque = YES;
    self.font = [UIFont boldSystemFontOfSize:44.0];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)setFont:(UIFont *)font {
    if (font == _font) return;
    
    _font = font;
    
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    if (text == _text) return;
    
    _text = [text copy];
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGRect bounds = self.bounds;
    
    NSUInteger length = self.text.length;
    unichar chars[length];
    CGGlyph glyphs[length];
    CGPoint positions[length];
    CGSize advances[length];
    
    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(bounds.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    CGRect textRect = CGRectMake(floor((bounds.size.width - size.width)/2.0),
                                 44.0,
                                 size.width,
                                 size.height);    
    
    [self.text getCharacters:chars range:NSMakeRange(0, length)];
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    
    CTFontGetGlyphsForCharacters(font, chars, glyphs, length);
    CTFontGetAdvancesForGlyphs(font, kCTFontDefaultOrientation, glyphs, advances, length);
    
    CGPoint position = textRect.origin;
    for (NSUInteger i=0; i<length; i++) {
        positions[i] = CGPointMake(position.x, position.y);
        CGSize advance = advances[i];
        position.x += advance.width;
        position.y += advance.height;
    }
    
    CGContextSaveGState(context);
    
    CGRect boundingBox = CTFontGetBoundingRectsForGlyphs(font, kCTFontDefaultOrientation, glyphs, NULL, length);
    CGContextTranslateCTM(context, 0.0, textRect.origin.y);
    CGContextTranslateCTM(context, 0.0, boundingBox.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -textRect.origin.y);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 10.0);
    
    for (NSUInteger i=0; i<length; i++) {
        CGPoint position = positions[i];
        CGAffineTransform tt = CGAffineTransformMakeTranslation(position.x, position.y);
        CGPathRef path = CTFontCreatePathForGlyph(font, glyphs[i], &tt);
        CGContextAddPath(context, path);
        CGPathRelease(path);        
    }

    CGContextStrokePath(context);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CTFontDrawGlyphs(font, glyphs, positions, length, context);    
    
    CGContextRestoreGState(context);
    
    CFRelease(font);

    CGContextRestoreGState(context);    
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值