iOS UIView 单独设置一个角为圆角,两个 三个角也行

摘要:
效果图:如下所示调用方法两句话很简单:-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(100,100,200,100)];label.backg

效果图:

iOS UIView 单独设置一个角为圆角,两个 三个角也行第1张

如下所示调用方法两句话 很简单:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
    label.backgroundColor = [UIColor blueColor];
    [self.view addSubview:label];
    
    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight;
    [label setBorderWithCornerRadius:50 borderWidth:10 borderColor:[UIColor redColor] type:corners];
    

    
}

UIView 类别里面的实现:

@implementation UIView (lcAnyCorners)

- (void)setBorderWithCornerRadius:(CGFloat)cornerRadius
                      borderWidth:(CGFloat)borderWidth
                      borderColor:(UIColor *)borderColor
                             type:(UIRectCorner)corners {
    
    //    UIRectCorner type = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft;
    
    //1. 加一个layer 显示形状
    CGRect rect = CGRectMake(borderWidth/2.0, borderWidth/2.0,
                             CGRectGetWidth(self.frame)-borderWidth, CGRectGetHeight(self.frame)-borderWidth);
    CGSize radii = CGSizeMake(cornerRadius, borderWidth);
    
    //create path
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    
    //create shape layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = borderColor.CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    shapeLayer.lineWidth = borderWidth;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:shapeLayer];
    
    
    
    
    //2. 加一个layer 按形状 把外面的减去
    CGRect clipRect = CGRectMake(0, 0,
                                 CGRectGetWidth(self.frame)-1, CGRectGetHeight(self.frame)-1);
    CGSize clipRadii = CGSizeMake(cornerRadius, borderWidth);
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:clipRect byRoundingCorners:corners cornerRadii:clipRadii];
    
    CAShapeLayer *clipLayer = [CAShapeLayer layer];
    clipLayer.strokeColor = borderColor.CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;

    clipLayer.lineWidth = 1;
    clipLayer.lineJoin = kCALineJoinRound;
    clipLayer.lineCap = kCALineCapRound;
    clipLayer.path = clipPath.CGPath;

    self.layer.mask = clipLayer;
}

@end

github 地址:https://github.com/lc081200/AnyCornersExample

免责声明:文章转载自《iOS UIView 单独设置一个角为圆角,两个 三个角也行》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS 键盘 隐藏系统的 toolBarArcGIS 网络分析[1.5] 使用点线数据一起创建网络数据集(如何避免孤立点/点与线的连通性组合结果表)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ios 给UIView添加背景图片

为了适应不同大小屏幕,一般给的背景图片都是@2x.png格式的。 我开始用的方法是 UIImage*imgMusic=[UIImageimageNamed:@"background@2x.png"]; self.backgroundColor=[UIColorcolorWithPatternImage:img]; 这样导致的后果是,背景图上很多毛须须。原因...

UIBezierPath精讲

前言 笔者在写本篇文章之前,也没有系统学习过贝塞尔曲线,只是曾经某一次的需求需要使用到,才临时百度看了一看而且使用最基本的功能。现在总算有时间停下来好好研究研究这个神奇而伟大的贝塞尔先生! 笔者在学习时,首先看了两遍UIBezierPath类头文件定义,熟悉了一下相关的属性和方法。 基础知识 使用UIBezierPath可以创建基于矢量的路径,此类是Cor...

ios 动画系列之六------UIBezierPath贝塞尔弧线常用方法记

//根据一个矩形画曲线 + (UIBezierPath*)bezierPathWithRect:(CGRect)rect //根据矩形框的内切圆画曲线 + (UIBezierPath*)bezierPathWithOvalInRect:(CGRect)rect //根据矩形画带圆角的曲线 + (UIBezierPath*)bezierPathWith...

iOS AppIcon透明圆角导致的动画问题

今天设计师给了一个如下AppIcon,四边是透明的圆角区域。 这个图标在iOS14上正常显示没什么问题,但是当使用上滑手势让程序退到后台时,在程序缩小到图标的过程中,动画的4个脚都显示出了不正确的黑色块,十分难看。如下图: 后来发现,如果吧透明圆角去掉,直接使用方形,如下图,就可以解决问题。ios14系统会自动把图标切成带圆角的样式,显示在桌面上。...

ios中为视图添加圆角

1.使用 layer设置指定圆角 或者设定一个或几个圆角   码修 关注2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的ui君并不想给我切图。所以只有自己画了。由于很久没有用过这些知识,花了一些时间,故记入笔记。也可以用来画半圆。选择要画圆角的位置只需要改变枚举即可...

iOS开发 贝塞尔曲线UIBezierPath(后记)

使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 。 1:UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个OC封装。使用此类可以定义常见的圆形、多边形等...