iOS 自定义label

上面是xib创建出来的,下面是纯代码创建的。

CULabel.h

@interface CULabel : UILabel
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) CGFloat triangleHeight;
@property (nonatomic, assign) CGFloat triangleWidth;
@end

CULabel.mm

@interface CULabel()
@property (nonatomic, strong) CAShapeLayer *maskLayer;
@property (nonatomic, strong) UIBezierPath *borderPath;
@end

@implementation CULabel


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

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

-(void)setup {
    self.maskLayer = [CAShapeLayer layer];
    [self.layer setMask:self.maskLayer];
    self.borderPath = [UIBezierPath bezierPath];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    CGFloat viewWidth = self.bounds.size.width;
    CGFloat viewHeigt = self.bounds.size.height;
    
    CGFloat radius = self.radius;
    CGFloat triangleWidth = self.triangleWidth;
    CGFloat triangleHeight = self.triangleHeight;
    
    [self.borderPath moveToPoint:CGPointMake(0, radius)];
    [self.borderPath addQuadCurveToPoint:CGPointMake(radius, 0) controlPoint:CGPointMake(0, 0)];
    [self.borderPath addLineToPoint:CGPointMake(viewWidth-radius, 0)];
    [self.borderPath addQuadCurveToPoint:CGPointMake(viewWidth, radius) controlPoint:CGPointMake(viewWidth, 0)];
    [self.borderPath addLineToPoint:CGPointMake(viewWidth, viewHeigt)];
    [self.borderPath addLineToPoint:CGPointMake(viewWidth/2.0+triangleWidth/2.0, viewHeigt)];
    [self.borderPath addLineToPoint:CGPointMake(viewWidth/2.0, viewHeigt-triangleHeight)];
    [self.borderPath addLineToPoint:CGPointMake(viewWidth/2.0-triangleWidth/2.0, viewHeigt)];
    [self.borderPath addLineToPoint:CGPointMake(0, viewHeigt)];
    [self.borderPath addLineToPoint:CGPointMake(0, radius)];
    
    self.maskLayer.path = self.borderPath.CGPath;
}


-(void)setRadius:(CGFloat)radius {
    _radius = radius;
    [self setNeedsDisplay];
}
-(void)setTriangleWidth:(CGFloat)triangleWidth {
    _triangleWidth = triangleWidth;
    [self setNeedsDisplay];
}
-(void)setTriangleHeight:(CGFloat)triangleHeight {
    _triangleHeight = triangleHeight;
    [self setNeedsDisplay];
}

@end

如何使用?

#import "CULabel.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet CULabel *clabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CULabel *label = [[CULabel alloc] initWithFrame:CGRectMake(40, 100, 200, 40)];
    [self.view addSubview:label];
    label.backgroundColor = [UIColor purpleColor];
    label.text = @"卖家推荐 * 新品推荐";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"" size:15];
    label.radius = 10;
    label.triangleWidth = 10.0;
    label.triangleHeight = 5.0;
    
    self.clabel.radius = 10;
    self.clabel.triangleWidth = 10.0;
    self.clabel.triangleHeight = 5.0;
}

@end

你可能感兴趣的:(iOS 自定义label)