iOS:OC--封装工具类(UILabel,UIButton, UIImageView ,UIAlertController)

封装UIButton
UIButton
#import <UIKit/UIKit.h>

@interface UIButton (Creat)

-(UIButton *)creatButtonWithFrame:(CGRect)frame backGround:(UIColor *)color title:(NSString *)title target:(id)target action:(SEL)action;
//SEL修饰方法类型,代表此参数是个方法
@end

#import "UIButton+Creat.h"

@implementation UIButton (Creat)

-(UIButton *)creatButtonWithFrame:(CGRect)frame backGround:(UIColor *)color title:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc]initWithFrame:frame];
    
    button.backgroundColor = color;
    
    [button setTitle:title forState:(UIControlStateNormal)];
    
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

@end

封装UILable
#import <UIKit/UIKit.h>

@interface UILabel (Creat)

-(UILabel *)createLableWithFrame:(CGRect)cg andFont:(CGFloat)font andTextalignment:(NSInteger)alignment andTextColor:(UIColor *)color andText:(NSString *)text;

@end
#import "UILabel+Creat.h"

@implementation UILabel (Creat)

-(UILabel *)createLableWithFrame:(CGRect)cg andFont:(CGFloat)font andTextalignment:(NSInteger)alignment andTextColor:(UIColor *)color andText:(NSString *)text
{
    UILabel *lable = [[UILabel alloc]init];
    
    lable.frame = cg;
    
    lable.font = [UIFont systemFontOfSize:font];
    
    lable.userInteractionEnabled = YES;
    
    lable.textAlignment = alignment;
    
    lable.textColor = color;
    
    lable.text = text;
    
    return lable;
    
}
@end
封装UIImageView
#import <UIKit/UIKit.h>

@interface UIImageView (Creat)

-(UIImageView *)creatImageViewWithFrame:(CGRect)cg andImageName:(NSString *)name;

@end
#import "UIImageView+Creat.h"

@implementation UIImageView (Creat)

-(UIImageView *)creatImageViewWithFrame:(CGRect)cg andImageName:(NSString *)name
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cg];
    
    imageView.userInteractionEnabled = YES;
    
    imageView.image = [UIImage imageNamed:name];
    
    return imageView;
}

@end
封装UIAlertController
#import <UIKit/UIKit.h>

@interface UIAlertController (Creat)

-(void)alertWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSArray *)buttonTitle confirm:(void(^)(void))ok cancel:(void(^)(void))cancel inController:(UIViewController *)controller;

@end
#import "UIAlertController+Creat.h"

@implementation UIAlertController (Creat)

-(void)alertWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSArray *)buttonTitle confirm:(void(^)(void))ok cancel:(void(^)(void))cancel inController:(UIViewController *)controller
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
    
    NSString *str = buttonTitle[0];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:str style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if (ok) {
            ok();//处理
        }
    }];
    
    [alertController addAction:action];
    
    str = buttonTitle[1];
    
    action = [UIAlertAction actionWithTitle:str style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if (cancel) {
            cancel();//处理
        }
    }];
    
    [alertController addAction:action];
    
    [controller presentViewController:alertController animated:YES completion:nil];
    
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容