美文网首页
ios 基于UILABEL的文字描边

ios 基于UILABEL的文字描边

作者: LV大树 | 来源:发表于2017-07-31 15:35 被阅读923次

    import <UIKit/UIKit.h>

    @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的文字描边

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