纯代码实现UITextField的圆角效果

需要为UITextField新建一个分类,新建一个函数,利用UIBezierPath中的bezierPathWithRoundedRect方法建立一个圆角矩形遮罩即可,显示效果非常好。

其中corners可以指定哪个角为圆角。

注意此方法只可设置输入区域为圆角,不能设置border

- (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius

{

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

    

    CAShapeLayer *maskLayer = [CAShapeLayer layer];

    maskLayer.frame = self.bounds;

    maskLayer.path = maskPath.CGPath;

    

    self.layer.mask = maskLayer;

}

 

设置border可用下面代码

_searchInputTextField.layer.cornerRadius=15.0f;

_searchInputTextField.layer.masksToBounds=YES;

posted @ 2016-04-12 17:12  Thkeer  阅读(743)  评论(0编辑  收藏  举报