如何优雅的给iOS控件的四个角设置是否有圆角

有时候纯色的背景如果让美工去做图的话,又是等啊等,又要撕逼,所以遇到纯色背景要做圆角的还是自己动手好。

例如要写一个绑定手机号,获取验证码的代码

如何优雅的给iOS控件的四个角设置是否有圆角_第1张图片
Screen Shot 2017-07-25 at 11.40.47.png

代码如下

@interface LFBindingPhoneController ()
/** text code*/
@property (strong, nonatomic) UITextField * testCodeTF;
/** test btn*/
@property (strong, nonatomic) UIButton * testCodeBtn;
@end
- (UITextField *)testCodeTF{
    if (!_testCodeTF) {
        _testCodeTF = [[UITextField alloc] initWithFrame:CGRectMake(W(12), H(75), self.view.width_ - 2 * W(12) - W(100), H(37))];
        _testCodeTF.placeholder = @"请输入验证码";
        _testCodeTF.borderStyle = UITextBorderStyleNone;
        _testCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _testCodeTF.backgroundColor = kRGB(238, 238, 238, 1);
        _testCodeTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _testCodeTF.returnKeyType = UIReturnKeyDone;
        _testCodeTF.delegate = self;
        _testCodeTF.tag = 200;
        // 设置左边圆角
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:_testCodeTF.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(9, 9)];
        CAShapeLayer * shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.frame = _testCodeTF.bounds;
        shapeLayer.path = path.CGPath;
        _testCodeTF.layer.mask = shapeLayer;
        // 设置左边图片
        UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, W(15), W(44), H(15))];
        leftImageView.image = [UIImage imageNamed:@"验证"];
        leftImageView.contentMode = UIViewContentModeScaleAspectFit;
        [_testCodeTF setLeftView:leftImageView];
        _testCodeTF.leftViewMode = UITextFieldViewModeAlways;
    }
    return _testCodeTF;
}

- (UIButton *)testCodeBtn{
    if (!_testCodeBtn) {
        CGRect rect = CGRectMake(CGRectGetMaxX(self.testCodeTF.frame), H(75), W(100), H(37));
        _testCodeBtn = [UIButton createWithTitle:@"获取验证码" textColorNormal:[UIColor whiteColor] textColorSelected:[UIColor lightGrayColor] font:[UIFont fontWithName:kFontName size:W(11)] frame:rect];
        _testCodeBtn.backgroundColor = kMainColor;
        // 设置右边圆角
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:_testCodeBtn.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(9, 9)];
        CAShapeLayer * shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.frame = _testCodeBtn.bounds;
        shapeLayer.path = path.CGPath;
        _testCodeBtn.layer.mask = shapeLayer;
        // 设置事件
        [_testCodeBtn addTarget:self action:@selector(postTestCode:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _testCodeBtn;
}  
    
        ```

你可能感兴趣的:(如何优雅的给iOS控件的四个角设置是否有圆角)