OC代码小技巧

OC中批量给控件设定参数可以这样写

   UILabel *(^makeLabel)() = ^{
        UILabel *label = [UILabel new];
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor whiteColor];
        return label;
    };
    
    UIButton *iconButton = [UIButton new];
    [self addSubview:self.iconButton = iconButton];
    
    [self addSubview:self.nameLabel = makeLabel()];
    [self addSubview:self.summaryLabel = makeLabel()];
    [self addSubview:self.blogCountLabel = makeLabel()];
    [self addSubview:self.friendCountLabel = makeLabel()];
    self.summaryLabel.textAlignment = NSTextAlignmentLeft;

你可能感兴趣的:(OC代码小技巧)