1. 最近刚接到一个任务: 重构公司的一个很老的项目......
  2. 在对项目进行代码走读是,发现很多基础UI控件都没有封装,所以自己封装了一个基础的UILabel、UIButton控件类
    1.   .h文件内容
      #import <Foundation/Foundation.h>
      
      NS_ASSUME_NONNULL_BEGIN
      
      @interface Bt_BaseUI : NSObject
      
      + (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text fontSize:(CGFloat)fontSize textAlignment:(NSTextAlignment)textAlignment;
      
      + (UIButton *)createButtonWithFrame:(CGRect)frame target:(id)target title:(NSString *)title action:(SEL)action bgImage:(NSString *)bgImage normalImage:(NSString *)normalImage selectImage:(NSString *)selectImage;
      
      @end
      
      NS_ASSUME_NONNULL_END

       

    2.   .m文件内容
      /************************************************
       Input:           target:调用此函数的对象;action:button的点击事件;
       Output:          一个UIButton类型的button;
       Return:          一个UIButton类型的button;
       Others:          无;
       Function:        类方法;
       Description:     根据传入的参数,自定义创建一个button;
       Calls:           无;
       Called By:       项目中需要创建button的地方;
       ***********************************************/
      + (UIButton *)createButtonWithFrame:(CGRect)frame target:(id)target title:(NSString *)title action:(SEL)action bgImage:(NSString *)bgImage normalImage:(NSString *)normalImage selectImage:(NSString *)selectImage{
          UIButton *button = [[UIButton alloc] initWithFrame:frame];
          [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
          [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
          if (title) {
              [button setTitle:title forState:UIControlStateNormal];
          }
          if (bgImage) {
              [button setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
          }
          if (normalImage) {
              [button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
          }
          if (selectImage) {
              [button setImage:[UIImage imageNamed:selectImage] forState:UIControlStateNormal];
          }
          return button;
      }
      
      /************************************************
       Input:           无;
       Output:          一个UILabel类型的label;
       Return:          一个UILabel类型的label;
       Others:          无;
       Function:        类方法;
       Description:     根据传入的参数,自定义创建一个label;
       Calls:           无;
       Called By:       项目中需要创建label的地方;
       ***********************************************/
      + (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text fontSize:(CGFloat)fontSize textAlignment:(NSTextAlignment)textAlignment{
          UILabel *label = [[UILabel alloc] initWithFrame:frame];
          if (text) {
              label.text = text;
          }
          if (fontSize != 0) {
              label.font = [UIFont systemFontOfSize:fontSize];
          }
          label.textAlignment = textAlignment;
          return label;
      }

       

      SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
    3.       调用
      - (void)viewDidLoad {
          [super viewDidLoad];
          [self createUI];
      }
      
      /************************************************
       Input:           无;
       Output:          无;
       Return:          无;
       Others:          创建基础界面;
       Function:        无;
       Description:     创建基础界面;
       Calls:           UIKIT中部分控件;
       Called By:       ViewDidload();
       ***********************************************/
      - (void)createUI{
          UIButton *button = [UITools createButtonWithFrame:CGRectMake(100, 100, 100, 50) target:self title:@"test" action:@selector(buttonClick:) bgImage:@"1" normalImage:@"" selectImage:@""];
          [self.view addSubview:button];
          
          UILabel *label = [UITools createLabelWithFrame:CGRectMake(100, 300, 100, 50) text:@"xxxx" fontSize:19 textAlignment:NSTextAlignmentCenter];
          [self.view addSubview:label];
      }
      
      /************************************************
       Input:           uibutton类型的参数;
       Output:          无;
       Return:          无;
       Others:          无;
       Function:        无;
       Description:     button点击方法测试类;
       Calls:           无;
       Called By:       无;
       ***********************************************/
      - (void)buttonClick:(UIButton *)sender {
          NSLog(@"sender.tag == %ld",(long)sender.tag);
      }

       

      end!

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄