UIView的任意圆角

今天在做项目的时候,遇到一个问题,grouped类型的tableview 怎么样才能让他们的一个view 其中一个角圆角?

如上图所示,其实我是用UILabel,但是箭头的位置总是尖的不太好看.设置layer.border 又让其他的角也圆了.有没有办法只让指定的角圆,并圆成合适的大小呢?

答案是有的..下面不废话,直接上代码了.

新建一个category  : UIVew+Roundify

 1 //

 2 //  UIVew+Roundify.h

 3 //  TNS4Golfers

 4 //

 5 //  Created by yingkong1987 on 13-9-14.

 6 //  Copyright (c) 2013年 yingkong1987. All rights reserved.

 7 //

 8 

 9 

10 

11 @interface UIView (Roundify)

12 -(void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii;

13 -(CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii;

14 @end
 1 //

 2 //  UIVew+Roundify.m

 3 //  TNS4Golfers

 4 //

 5 //  Created by yingkong1987 on 13-9-14.

 6 //  Copyright (c) 2013年 yingkong1987. All rights reserved.

 7 //

 8 

 9 #import "UIVew+Roundify.h"

10 

11 @implementation UIView (Roundify)

12 -(void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {

13     CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii];

14     self.layer.mask = tMaskLayer;

15 }

16 

17 -(CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii {

18     CAShapeLayer *maskLayer = [CAShapeLayer layer];

19     maskLayer.frame = self.bounds;

20 

21     UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:

22                                                                  maskLayer.bounds byRoundingCorners:corners cornerRadii:radii];

23     maskLayer.fillColor = [[UIColor whiteColor] CGColor];

24     maskLayer.backgroundColor = [[UIColor clearColor] CGColor];

25     maskLayer.path = [roundedPath CGPath];

26 

27     return maskLayer;

28 }

29 @end

然后使用如下如:

效果出来了:

 

当然如果有更好更快捷的做法,欢迎大家拍砖,并指教指教小弟!~~小弟初学iOS

你可能感兴趣的:(UIView)