HCViewEasyMake 0.1.0

HCViewEasyMake 0.1.0

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布上次发布2014年12月

Heechul Ryu维护。



创建、添加、移除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];
})

发布

两行代码;我们经常忘记调用removeFromSuperView方法;

[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文件。