封装Label,TextField

1.创建UIView
2.在UIView.h中创建两个属性

@property UILabel *aLabel;
@property UITextField *textField;

3.在UIView.m中重写初始化方法.创建新方法

//重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
        [self setLabel];
        [self setTextField];
    }
    return self;
}
//模块化创建label
//野指针 就是没有指向确定空间的指针
- (void)setLabel {
    _aLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
    _aLabel.backgroundColor = [UIColor brownColor];
    _aLabel.textColor = [UIColor cyanColor];
    [self addSubview:_aLabel];
}
- (void)setTextField{
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(130, 10, 250, 40)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;//默认白色
    _textField.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:_textField];
}

4.在RootViewController.m的viewDidLoad中调用LTView

 LTView *aView = [[LTView alloc]initWithFrame:CGRectMake(10, 50, 390, 60)];
    aView.backgroundColor = [UIColor yellowColor];
    aView.aLabel.text = @"账号";
    aView.textField.placeholder = @"请输入账号";
    [self.view addSubview:aView];

你可能感兴趣的:(封装Label,TextField)