iOS关于圆角

1.简单的view.cornerRadius这里就不说了
缺点:性能消耗大,只能同时定义四个角。
2.看看好用的方法
使用UIBezierPath和CAShapeLayer
(只画上面两个圆角)

UILabel * lab = [[UILabel  alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-18, 42)];
    lab.text = @"内容";
    lab.textAlignment = NSTextAlignmentCenter;
    lab.textColor = [UIColor colorWithRGBHex:0xf57d4b];
    lab.font = [UIFont systemFontOfSize:15];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:lab.bounds      byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight   cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = lab.bounds;
    maskLayer.path = maskPath.CGPath;
    lab.layer.mask = maskLayer;

使用这种方式可以轻松自由的定制四个不同角的曲率,性能消耗上也要小很多。
以上两种方式一般已经足够日常开发使用了,切圆角的动作非常频繁的话,也可以用另一种追求极致性能体验的方法:
使用UIBezierPath和Core Graphics框架画出一个圆角

 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 200, 200)];
    imageView.image = [UIImage imageNamed:@"img"];
    //对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
    //用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];

    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
     //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imageView];

你可能感兴趣的:(iOS关于圆角)