iOS UI 快捷创建封装

作者: CodeGeass | 来源:发表于2016-06-29 11:13 被阅读407次

封装了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 快捷创建封装

    封装了UIView、UILabel、UIButton、UIImageView的快速创建方法,让我们不用每次都进行繁...

  • 便利构造器

    遍历构造器封装了对象创建的过程: 内部实现:封装了alloc和初始化操作,创建对象更加方便快捷. .h文件中: ....

  • iOS XIB运用技巧和原理

    iOS开发的这些年里,有的人用代码创建UI,有的人用xib创建UI。到底是用xib还是代码来创建UI,这个问题以前...

  • React Native调用iOS原生UI组件的方法

    在RN中文网的原生UI组件章节: iOS Android,我们知道如何封装原生UI组件给Javascript 端使...

  • flutter脚手架

    flutter_app 1.目录结构 github源码地址 2.功能点封装 网络请求 dio封装 快捷创建解析实体...

  • iOS创建Framework、Bundle

    iOS创建Framework、Bundle 创建FrameworkB 1.打开Xcode,新建工程(快捷键,shi...

  • ios封装UI组件

    RCTVideoPlayer 假定原生组件是一个View,在ios端封装该View 1.1 导入头文件

  • ios 动态创建和复用结构

    在iOS开发中,UI对象的创建我一值坚持的动态创建对象。顶部菜单为例根据数据完成UI对象的创建,动态完成和修改。保...

  • IOS 中 NS CG ,UI 之间的区别。如UIFont C

    UI是iOS的UI库,用objective-c封装的,一般用于普通的视图和控制器,如UIView、UIImageV...

  • iOS button addTarget 无法响应事件

    iOS button addTarget 无法响应事件 1.问题描述 封装了一个XYAlterview,继承于UI...

网友评论

    本文标题:iOS UI 快捷创建封装

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