iOS第三方资料收集封装iOS开发实用技术

iOS开发小笔记 | 封装一个带左内边距的UITextField

2017-03-21  本文已影响515人  Lol刀妹
很强势.png

注:此控件继承自UITextField

#pragma mark - 构造方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // TextField的左视图
        UIView *leftView = [[UIView alloc]init];
        [self addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.left.mas_offset(0);
            make.bottom.mas_offset(0);
            make.width.equalTo(self.mas_height).multipliedBy(0.5);
        }];
        
        // 属性设置
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1;
        self.leftViewMode = UITextFieldViewModeAlways;
        self.leftView = leftView;
    }
    return self;
}

#pragma mark - 调整UI
- (void)layoutSubviews{
    [super layoutSubviews];
    // 调整圆角大小
    self.layer.cornerRadius = self.frame.size.height / 2;
}

上面效果图的代码

    LoginViewCustomTextField *textField1 = [[LoginViewCustomTextField alloc]initWithFrame:CGRectMake(90, 90, 200, 40)];
    [self.view addSubview:textField1];
    
    LoginViewCustomTextField *textField2 = [[LoginViewCustomTextField alloc]initWithFrame:CGRectMake(90, 150, 80, 20)];
    [self.view addSubview:textField2];
    
    LoginViewCustomTextField *textField3 = [[LoginViewCustomTextField alloc]init];
    [self.view addSubview:textField3];
    [textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(90);
        make.top.mas_equalTo(textField2.mas_bottom).mas_offset(20);
        make.right.mas_offset(-90);
        make.height.mas_equalTo(60);
    }];
上一篇下一篇

猜你喜欢

热点阅读