内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

iOS自定义带两个圆角的UILabel

2019-03-05 21:13 出处:清屏网 人气: 评论(0

这里要实现的是带有两个圆角的自定义的UIlabel

  • 两个圆角的UIlabel.png

  • 主要思路是利用贝塞尔曲线绘制masklayer的轨迹。

    下面直接看代码:

#import "CustomizeLabel.h"@interface CustomizeLabel()@property (nonatomic, strong)CAShapeLayer *maskLayer;@property (nonatomic, strong)UIBezierPath *borderPath;@end@implementation CustomizeLabel- (instancetype)initWithFrame:(CGRect)frame{    
    self = [super initWithFrame:frame];    if(self){
        
        _maskLayer = [CAShapeLayer layer];
        [self.layer setMask:_maskLayer];        
        self.borderPath = [UIBezierPath bezierPath];
    }    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];    
    // 遮罩层frame
    self.maskLayer.frame = self.bounds;    
    // 设置path起点
    [self.borderPath moveToPoint:CGPointMake(0, 10)];    // 左上角的圆角
    [self.borderPath addQuadCurveToPoint:CGPointMake(10, 0) controlPoint:CGPointMake(0, 0)];    //直线,到右上角
    [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width, 0)];    //直线,到右下角
    [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height-10)];    //右下角的圆角
    [self.borderPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width-10, self.bounds.size.height) controlPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];    //底部的小三角形
    [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width/2.0 +5, self.bounds.size.height)];
    [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height - 5)];
    [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width/2.0 -5, self.bounds.size.height)];    //直线到左下角
    [self.borderPath addLineToPoint:CGPointMake(0, self.bounds.size.height)];    //直线,回到起点
    [self.borderPath addLineToPoint:CGPointMake(0, 10)];        
    // 将这个path赋值给maskLayer的path
    self.maskLayer.path = self.borderPath.CGPath;
}@end
  • 使用:

    CustomizeLabel *label = [[CustomizeLabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
    label.text = @"现在我们来测试一下这个自定义的按钮";
    label.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:label];
分享给小伙伴们:
本文标签: UILabeliOS

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.