创建、添加、移除UIView,使用更少的模板代码。
#define HCViewOnceGetter(class, property, ivar, internalVar, block)
#define HCViewRelease(VIEW)
- (void)addSubviews:(NSArray *)subviews;
- (void)setSize:(CGSize)size;
- (void)setOrigin:(CGPoint)origin;
- (CGFloat)xPlusWidh;
- (CGFloat)yPlusHeight;
- (UILabel *)label
{
if (!_label) {
_label = ({
UILabel *lbl = [[UILabel alloc] init];
// you can add something for this label finally.. from here
lbl.font = [UIFont systemFontOfSize:12];
lbl.text = @"too much boiler plate code";
[lbl sizeToFit];
// to here
lbl;
});
}
return _label;
}
HCViewOnceGetter(UILabel, label, _label, lbl, ^{
lbl.font = [UIFont systemFontOfSize:12];
lbl.text = @"clean and easy view creation";
[lbl sizeToFit];
})
[self.label removeFromSuperview];
self.label = nil;
HCViewRelease(self.label)
@implementation HCExampleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self addSubviews:@[self.label, self.imgView, self.button]];
[self.label setOrigin:CGPointMake(10,10)];
[self.imgView setOrigin:CGPointMake(10, self.label.yPlusHeight + 10)];
[self.button setOrigin:CGPointMake(self.imgView.xPlusWidh + 10, self.label.yPlusHeight + 10)];
}
return self;
}
HCViewOnceGetter(UILabel, label, _label, lbl, ^{
lbl.font = [UIFont systemFontOfSize:12];
lbl.text = @"clean and easy view creation";
[lbl sizeToFit];
})
HCViewOnceGetter(UIImageView, imgView, _imgView, imgV, ^{
imgV.image = [UIImage imageNamed:@"someImage"];
imgV.backgroundColor = [UIColor blackColor];
[imgV setSize:CGSizeMake(50, 50)];
})
HCViewOnceGetter(UIButton, button, _button, btn, ^{
[btn setTitle:@"Release Label and ImageView" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
})
- (void)touchButton:(id)sender
{
HCViewRelease(self.label)
HCViewRelease(self.imgView)
}
@end
@implementation HCExampleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self addSubview:self.label];
[self addSubview:self.imgView];
[self addSubview:self.button];
CGRect newFrame = self.label.frame;
newFrame.origin = CGPointMake(10,10);
self.label.frame = newFrame;
newFrame = self.imgView.frame;
newFrame.origin = CGPointMake(10, self.label.frame.origin.y + self.label.frame.size.height + 10);
self.imgView.frame = newFrame;
newFrame = self.button.frame;
newFrame.origin = CGPointMake(self.imgView.frame.origin.x + self.imgView.frame.size.width+ 10, self.label.frame.origin.y + self.label.frame.size.height + 10);
self.button.frame = newFrame;
}
return self;
}
- (UILabel *)label
{
if (!_label) {
_label = ({
UILabel *lbl = [[UILabel alloc] init];
lbl.font = [UIFont systemFontOfSize:12];
lbl.text = @"clean and easy view creation";
[lbl sizeToFit];
lbl;
});
}
return _label;
}
- (UIImageView *)imgView
{
if (!_imgView) {
_imgView = ({
UIImageView *imgV = [[UIImageView alloc] init];
imgV.image = [UIImage imageNamed:@"someImage"];
imgV.backgroundColor = [UIColor blackColor];
CGRect newFrame = imgV.frame;
newFrame.size = CGSizeMake(50, 50);
imgV.frame = newFrame;
imgV;
});
}
return _imgView;
}
- (UIButton *)button
{
if (!_button) {
_button = ({
UIButton *btn = [[UIButton alloc] init];
[btn setTitle:@"Release Label and ImageView" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
btn;
});
}
return _button;
}
- (void)touchButton:(id)sender
{
[self.label removeFromSuperview];
self.label = nil;
[self.imgView removeFromSuperview];
self.imgView = nil;
}
@end
实际上这是我第一次开源贡献,我也很乐意得到大家的贡献。
请发我Pull Requests或者提出想法让我改进!
要运行示例项目,首先克隆仓库,从Example目录运行pod install
。
Ryu Heechul,[email protected]
HCViewEasyMake在MIT许可下可用。有关更多信息,请参阅LICENSE文件。