美文网首页
OC代码小技巧

OC代码小技巧

作者: 小炯 | 来源:发表于2017-06-06 14:31 被阅读5次

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代码小技巧

      本文链接:https://www.haomeiwen.com/subject/ymwgfxtx.html