iOS 快速创建UI 工厂方法

//
//  SK_FactoryTool.h
//  SK_FactoryUI
//
//  Created by TrimbleZhang on 2019/1/23.
//  Copyright © 2019 AlexanderYeah. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN


@interface SK_FactoryTool : NSObject


/** MARK:创建一个UIView*/
+ (UIView *)creViewFrame:(CGRect)frame
                      color:(UIColor *)color
                 targetView:(UIView *)targetView;




/** MARK:创建一个UILabel*/
+ (UIView *)creLblFrame:(CGRect)frame
                     title:(NSString *)title
                  fontSize:(CGFloat)fontSize
                 alignment:(NSTextAlignment)alignment
                 textColor:(UIColor *)textColor
                 bgColor:(UIColor *)bgColor
                targetView:(UIView *)targetView;


/** MARK:创建一个UIButton*/
+ (UIButton *)creBtnFrame:(CGRect)frame
                       title:(NSString *)title
                  titleColor:(UIColor *)titleColor
                    fontSize:(CGFloat)fontSize
                     bgColor:(UIColor *)bgColor
             backgroundImage:(NSString *)imageStr
                  targetView:(UIView *)targetView;


/** MARK:创建一个UIImageView */
+ (UIImageView *)creImgViewFrame:(CGRect)frame
                           imageStr:(NSString *)imageStr
                         targetView:(UIView *)targetView;


/** MARK:创建一个UITextField */
+ (UITextField *)creTextFieldFrame:(CGRect)frame
                          fontSize:(CGFloat)fontSize
                            holder:(NSString *)holder
                        targetView:(UIView *)targetView;


/** MARK:创建一个UITextView */


+ (UITextView *)creTextViewFrame:(CGRect)frame
                         fontSize:(CGFloat)fontSize
                       targetView:(UIView *)targetView;


/** MARK:创建一个标题tablView header */
+ (UIView *)createLeftTitleHeaderViewWithTitle:(NSString *)title color:(UIColor *)color fontSize:(CGFloat)fontSize leftPadding:(CGFloat)leftPadding;


@end

NS_ASSUME_NONNULL_END

//
//  SK_FactoryTool.m
//  SK_FactoryUI

//  Copyright © 2019 AlexanderYeah. All rights reserved.
//

#import "SK_FactoryTool.h"

@implementation SK_FactoryTool


/** MARK:创建一个UIView*/
+ (UIView *)creViewFrame:(CGRect)frame
                   color:(UIColor *)color
              targetView:(UIView *)targetView
{
    UIView *view;
    if (CGRectEqualToRect(frame, CGRectZero)) {
        
        view = [[UIView alloc]init];
    }else{
        view = [[UIView alloc]initWithFrame:frame];
    }
    
    view.backgroundColor = color;
    [targetView addSubview:view];
    return view;
}


/** MARK:创建一个UILabel*/
+ (UIView *)creLblFrame:(CGRect)frame
                  title:(NSString *)title
               fontSize:(CGFloat)fontSize
              alignment:(NSTextAlignment)alignment
              textColor:(UIColor *)textColor
                bgColor:(UIColor *)bgColor
             targetView:(UIView *)targetView;
{
    
    UILabel *lbl;
    if (CGRectEqualToRect(frame, CGRectZero)) {
        lbl = [[UILabel alloc]init];
    }else{
        lbl = [[UILabel alloc]initWithFrame:frame];
    }    
    lbl.text = title;
    lbl.font = [UIFont systemFontOfSize:fontSize];
    lbl.textAlignment = alignment;
    lbl.textColor = textColor;
    lbl.backgroundColor = bgColor;
    [targetView addSubview:lbl];
    return lbl;
}


/** MARK:创建一个UIButton*/
+ (UIButton *)creBtnFrame:(CGRect)frame
                    title:(NSString *)title
               titleColor:(UIColor *)titleColor
                 fontSize:(CGFloat)fontSize
                  bgColor:(UIColor *)bgColor
          backgroundImage:(NSString *)imageStr
               targetView:(UIView *)targetView;
{
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    btn.backgroundColor = bgColor;
    [targetView addSubview:btn];
    
    if (imageStr.length != 0) {
        [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    }
    
    if (CGRectEqualToRect(frame,CGRectZero)) {
        btn.frame = CGRectZero;
    }else{
        btn.frame = frame;
    }
    
    return btn;

    
    
}


/** MARK:创建一个UIImageView */
+ (UIImageView *)creImgViewFrame:(CGRect)frame
                        imageStr:(NSString *)imageStr
                      targetView:(UIView *)targetView
{
    
    UIImageView *imgView;
    if (CGRectEqualToRect(frame, CGRectZero)) {
        imgView = [[UIImageView alloc]init];
    }else{
        imgView = [[UIImageView alloc]initWithFrame:frame];
    }

    imgView.image = [UIImage imageNamed:imageStr];
    [targetView addSubview:imgView];
    
    return imgView;
}



/** MARK:创建一个UITextField */
+ (UITextField *)creTextFieldFrame:(CGRect)frame
                          fontSize:(CGFloat)fontSize
                            holder:(NSString *)holder
                        targetView:(UIView *)targetView
{
    UITextField *textField;
    
    if (CGRectEqualToRect(frame, CGRectZero)) {
        textField = [[UITextField alloc]init];
    }else{
        textField = [[UITextField alloc]initWithFrame:frame];
    }
    
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = holder;
    textField.font = [UIFont systemFontOfSize:fontSize];
    [targetView addSubview:textField];
    
    return textField;
    
    
}


+ (UITextView *)creTextViewFrame:(CGRect)frame
                        fontSize:(CGFloat)fontSize
                      targetView:(UIView *)targetView
{
    
    UITextView *textView;
    if (CGRectEqualToRect(frame, CGRectZero)) {
        textView = [[UITextView alloc]init];
    }else{
        textView = [[UITextView alloc] initWithFrame:frame];
    }
    textView.font = [UIFont systemFontOfSize:fontSize];
    textView.layer.borderColor =[UIColor colorWithRed:210/255.f green:210/255.f blue:210/255.f alpha:1].CGColor;
    textView.layer.borderWidth = 0.6;
    textView.layer.cornerRadius = 2;
    textView.layer.masksToBounds = YES;
    [targetView addSubview:textView];
    
    return textView;
    
}

/** MARK:创建一个标题tablView header */

+ (UIView *)createLeftTitleHeaderViewWithTitle:(NSString *)title color:(UIColor *)color fontSize:(CGFloat)fontSize leftPadding:(CGFloat)leftPadding
{
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1];
    bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
 
    UIView *sepView1 = [[UIView alloc]init];
    sepView1.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    sepView1.frame = CGRectMake(0, 49, SCREEN_WIDTH, 1);
    [bgView addSubview:sepView1];
    
    UILabel *titleLbl = [[UILabel alloc]init];
    titleLbl.font = [UIFont systemFontOfSize:fontSize];
    titleLbl.textAlignment = 0;
    titleLbl.textColor = color;
    titleLbl.text = title;
    titleLbl.userInteractionEnabled = YES;
    titleLbl.frame = CGRectMake(leftPadding, 0, SCREEN_WIDTH - leftPadding * 2, 49);
    [bgView addSubview:titleLbl] ;
    return bgView;
    
}



@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本框架适用于 使用 NavigationController UITabBarController 的APP 框架QLSNavTab , GitHub地址:https://github.com/qianlishun/QLSNavTab 简介   -------准备工作------- 在AppDelegate中 设置一个控制器为主控制器 , 例如MainController - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; MainController *mainVc = [[MainController alloc]init]; // 设置根控制器 self.window.rootViewController = mainVc; // 设置为主控制器并可见 [self.window makeKeyAndVisible]; return YES; } 复制代码 在主控制器 中 复制代码 // 设置Nav背景色 self.navigationBackgroundColor = [UIColor colorWithRed:arc4random_uniform (256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]; self.childControllerAndIconArr = @[ /************第一个控制器配置信息*********************/ @{ VC_VIEWCONTROLLER : [[OneController alloc]init], //控制器对象 NORMAL_ICON : @"icon_classTable", //正常状态的Icon 名称 SELECTED_ICON : @"icon_classTable_selected", //选中状态的Icon 名称 TITLE : @"表" //Nav和Tab的标题 }, /************第二个控制器配置信息*********************/ @{ VC_VIEWCONTROLLER : [[TwoController alloc]init], NORMAL_ICON : @"icon_me", SELECTED_ICON : @"icon_me_selected", TITLE : @"通讯录" }, @{ /* 如果在此处使用storyboard,需要给storyboard设置storyboardID storyboardID 与 VC_STORYBOARD的value 同名 此处为Three */ VC_STORYBOARD :@"Three", NORMAL_ICON : @"icon_discover", SELECTED_ICON : @"icon_discover_selected", TITLE : @"发现" }, ];

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值