ios 基于UILABEL的文字描边

import

@interface BorderLabel : UILabel

@property (nonatomic,strong)UIColor *borderColor;

@property (nonatomic,assign)CGFloat borderWidth;

@end

import "BorderLabel.h"

@implementation BorderLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {
    // Drawing code
    }
    */

//继承UILabel以后重载drawTextInRect

  • (void)drawTextInRect:(CGRect)rect {

    CGSize shadowOffset = self.shadowOffset;
    UIColor *textColor = self.textColor;

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, _borderWidth);
    CGContextSetLineJoin(c, kCGLineJoinRound);

    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = _borderColor;// [UIColor whiteColor];
    [super drawTextInRect:rect];

    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = textColor;
    self.shadowOffset = CGSizeMake(0, 0);
    [super drawTextInRect:rect];

    self.shadowOffset = shadowOffset;

}

@end

e.g.

BorderLabel *bo = [[BorderLabel  alloc]initWithFrame:CGRectMake(0, 300, 300, 100)];

[self.view addSubview:bo];
bo.text = @"AaBBBBBcccc";
bo.borderColor = [UIColor redColor];
bo.font = [UIFont systemFontOfSize:50];
bo.borderWidth = 4;

你可能感兴趣的:(ios 基于UILABEL的文字描边)