iOS UI 快捷创建封装

封装了UIView、UILabel、UIButton、UIImageView的快速创建方法,让我们不用每次都进行繁杂的UI代码的编写。引用了 UIGestureRecognizer (YYAdd)。

#pragma mark - For UIView
+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = bgColor;
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
{
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor];
    if (cornerRadius > 0) {
        view.clipsToBounds = YES;
        view.layer.cornerRadius = cornerRadius;
    }
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                    actionGesture:(UIGestureRecognizer *)gesture
{
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius];
    [view addGestureRecognizer:gesture];
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                        tapAction:(void(^)())tapAction
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius actionGesture:tap];
    return view;
}

#pragma mark - For UILabel
+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    if (textAlignment) {
        label.textAlignment = textAlignment;
    }
    if (fontSize > 0) {
        label.font = [UIFont systemFontOfSize:fontSize];
    }
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
{
    UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize];
    label.textColor = textColor;
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
{
    UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor];
    label.backgroundColor = bgColor;
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
{
    UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor];
    if (cornerRadius > 0) {
        label.clipsToBounds = YES;
        label.layer.cornerRadius = cornerRadius;
    }
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                        tapAction:(void(^)())tapAction
{
    UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor cornerRadius:cornerRadius];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer:tap];
    return label;
}

#pragma mark - For UIButton
+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                             action:(void(^)())action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    if (fontSize > 0) {
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    [button addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if (action) {
            action();
        }
    }];
    return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                             action:(void(^)())action
{
    UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize action:action];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.backgroundColor = bgColor;
    return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                       cornerRadius:(CGFloat)cornerRadius
                             action:(void(^)())action
{
    UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize titleColor:titleColor bgColor:bgColor action:action];
    if (cornerRadius > 0) {
        button.clipsToBounds = YES;
        button.layer.cornerRadius = cornerRadius;
    }
    return button;
}

#pragma mark - For UIImageView
+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = [UIImage imageNamed:imageName];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                             cornerRadius:(CGFloat)cornerRadius
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    if (cornerRadius > 0) {
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = cornerRadius;
        imageView.layer.masksToBounds = YES;
    }
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
{
    UIImageView *imageView = [self createImageViewWithFrame:frame cornerRadius:cornerRadius];
    imageView.image = [UIImage imageNamed:imageName];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
                            actionGesture:(UIGestureRecognizer *)gesture
{
    UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:gesture];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
                                tapAction:(void(^)())tapAction
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius actionGesture:tap];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                              roundCorner:(BOOL)roundCorner
                                tapAction:(void(^)())tapAction
{
    CGFloat cornerRadius = 0;
    if (roundCorner) {
        cornerRadius = frame.size.width / 2;
    }
    return [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius tapAction:tapAction];
}

你可能感兴趣的:(iOS UI 快捷创建封装)