UI常用模版

UILabel

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"标题";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor blueColor];
        
        _titleLabel.numberOfLines = 13;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
//        _titleLabel.layer.backgroundColor = [UIColor blueColor].CGColor;
//        _titleLabel.layer.cornerRadius = 13;
//        _titleLabel.layer.masksToBounds = YES;
    }
    return _titleLabel;
}

UIButton

- (UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [UIButton new];
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _confirmButton.backgroundColor = [UIColor blueColor];
        
        [_confirmButton setImage:[UIImage imageNamed:@"闭眼"] forState:UIControlStateSelected];
        [_confirmButton setImage:[UIImage imageNamed:@"睁眼"] forState:UIControlStateNormal];
        _confirmButton.layer.cornerRadius = 13;
        
        _confirmButton.tag = 1300;
        [_confirmButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (void)buttonClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1300:
            //确认
            
            break;
        default:
            break;
    }
}

UIImageView

#define IMAGE_SIZE 13

- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        [_iconImageView setImage:[UIImage imageNamed:@"user_icon"]];
        //因为网络图片并不是等宽高的,所以你放入一个正方形会被拉伸压缩,需填充剪裁
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.clipsToBounds = YES;
        //圆角
        _iconImageView.layer.cornerRadius = IMAGE_SIZE/2;
        _iconImageView.layer.masksToBounds = YES;
        //点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconClick)];
        [_iconImageView addGestureRecognizer:tapGesture];
        _iconImageView.userInteractionEnabled = YES;
    }
    return _iconImageView;
}

- (void)iconClick{
    NSLog(@"你好骚哦");
}

UITableView

@property(strong,nonatomic) UITableView *tableView;
@property(strong,nonatomic) NSArray *dataSource;

static NSString *const CellID = @"CellID";
static NSString *HeaderID = @"HeaderID";

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 130;
        
        [_tableView registerClass:[TestTableViewCell class] forCellReuseIdentifier:CellID];
        [_tableView registerClass:[TestHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderID];
//        _tableView.tableHeaderView = self.headerView;

    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellModel *model = _dataSource[indexPath.row];
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    cell.model = model;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TestHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderID];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return KHeaderViewH ;
}

UITableViewCell

@class CellModel;
@property (nonatomic,strong) CellModel *model;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //去除cell选中色
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

UITableViewHeaderFooterView

#define KHeaderViewH         44

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor blueColor];
    }
    return self;
}

SectionFooterView同SectionHeaderView一样
不知道为啥,用masonry布局,SectionHeaderView不需要约束contentView就能充满整个布局
而TableHeaderView和SectionFooterView必须要约束ContentView,才能显示出来


你可能感兴趣的:(UI常用模版)