iOS自定义带两个圆角的UILabel

栏目: IOS · 发布时间: 5年前

内容简介:这里要实现的是带有两个圆角的自定义的UIlabel

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

iOS自定义带两个圆角的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];

作者:雪山飞狐_91ae

链接:https://www.jianshu.com/p/04abe5a5d02f


以上所述就是小编给大家介绍的《iOS自定义带两个圆角的UILabel》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具