使用CoreText画文字的边框
  uVC9tksnm5fA 2023年11月02日 18 0


#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



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  PVcilKyJJTzb   2023年11月02日   32   0   0 git推送github
  EhkezVjvcUv6   2023年11月02日   40   0   0 #includei++测试数据
  Fv5flEkOgYS5   2023年11月02日   37   0   0 i++javaide
  Mqh2iumZ9USt   2023年11月02日   30   0   0 #includei++ios
uVC9tksnm5fA