ios开发——给uiview等设置圆角


//设置圆角

    zheZhaoView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 120, 120)];

    [zheZhaoView setBackgroundColor:[UIColor colorWithWhite:0.3f alpha:0.8f]];

    zheZhaoView.layer.cornerRadius = 10;//设置那个圆角的有多圆

    zheZhaoView.layer.borderWidth = 0;//设置边框的宽度,当然可以不要

    zheZhaoView.layer.borderColor = [[UIColor grayColor] CGColor];//设置边框的颜色

    zheZhaoView.layer.masksToBounds = YES;

    [self addSubview:zheZhaoView]





如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。

对于这种情况,Stackoverflow上提供了几种解决方案。其中最简单优雅的方案,就是使用UIBezierPath。下面给出一段示例代码。

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
    
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;

其中,

byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight

指定了需要成为圆角的角。该参数是UIRectCorner类型的,可选的值有:

* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners

从名字很容易看出来代表的意思,使用“|”来组合就好了。


感谢原作者简明扼要的总结!

原文地址:http://webfrogs.me/2013/05/22/ios-view-assign-corner-radius/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值