Skip to content

zsy78191/UIView-Maker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UIView-Maker

A category for UIView to copy and reuse.

How to copy UIView ? Use NSKeyedArchiver/NSKeyedUnarchiver

Core:

+ (__kindof UIView*)duplicate:(__kindof UIView*)view
{
    NSData * tempArchive = [NSKeyedArchiver archivedDataWithRootObject:view];
    return [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
}

Than, we can use - (__kindof UIView*)clone;to copy UIIView

- (UIView *)clone
{
    return [[self class] duplicate:self];
}

Regist standard UIView as model

We can set key, and use make block to set style of this view.

+ (void)registStyle:(NSString*)key make:(void(^)(id view))makeBlock;

for example :

[MDCButton registStyle:@"category_button" make:^(MDCButton*  _Nonnull view) {
        view.frame = CGRectMake(15, 30, 50, 50);
        [view setBackgroundColor:[UIColor add_colorWithRGBHexString:@"#178EDA"] forState:UIControlStateNormal];
    }];

when we need use category_button

MDCButton* clone = [MDCButton cloneForKey:@"category_button"];

Layout of views with same style

Use copyTimesfunction to layout views with same style :

- (void)copyTimes:(NSUInteger)times make:(void(^)(id view, NSUInteger idx))makeBlock;

for example:

UILabel* label = [[UILabel alloc] init];
    [label copyTimes:10 make:^(id  _Nonnull view, NSUInteger idx) {
      [self.view addSubview:view];
      [view setFrame:CGRectMake(0, idx * 100, 50, 100)];
}];

About

A category for UIView to copy and reuse.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published