美文网首页
封装的button和image的创建工具类

封装的button和image的创建工具类

作者: 破夕_____________ | 来源:发表于2017-06-26 15:52 被阅读11次

    .h文件

    //
    //  Tools.h
    
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface Tools : NSObject
    
    /**
     *  通过文件路径加载图片
     *  该方法加载图片优势:不会将图片加到内存缓存中(适用类型:较大图片的处理)
     *
     *  @param imgName 图片名称(带扩展名)eg:btn_login.png
     *
     *  @return 返回图片对象
     */
    +(UIImage *)imageWithName:(NSString *)imgName;
    
    // 创建一个按钮 (以图片展现)
    +(UIButton *)createButtonNormalImage:(NSString *)normalImageName selectedImage:(NSString *)selectImageName tag:(NSUInteger)tag addTarget:(id)target action:(SEL)action;
    
    // 创建UILabel
    +(UILabel *)createLabelWithFrame:(CGRect)frame textContent:(NSString *)text withFont:(UIFont *)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)align;
    
    @end
    
    
    

    .m文件

    //
    //  Tools.m
    
    
    //
    
    #import "Tools.h"
    
    @implementation Tools
    
    /**
     *  通过文件路径加载图片
     *  该方法加载图片优势:不会将图片加到内存缓存中(适用类型:较大图片的处理)
     *
     *  @param imgName 图片名称(带扩展名)eg:btn_login.png
     *
     *  @return 返回图片对象
     */
    +(UIImage *)imageWithName:(NSString *)imgName
    {
        if(imgName){
            NSString * path = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
            UIImage * image = [UIImage imageWithContentsOfFile:path];
            return image;
        }
        
        return nil;
    }
    
    // 创建一个按钮 (以图片展现)
    +(UIButton *)createButtonNormalImage:(NSString *)normalImageName selectedImage:(NSString *)selectImageName tag:(NSUInteger)tag addTarget:(id)target action:(SEL)action
    {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateHighlighted];
        btn.tag = tag;
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        return btn;
    }
    
    // 创建UILabel
    +(UILabel *)createLabelWithFrame:(CGRect)frame textContent:(NSString *)text withFont:(UIFont *)font textColor:(UIColor *)color textAlignment:(NSTextAlignment)align
    {
        UILabel * label = [[UILabel alloc] initWithFrame:frame];
        label.font = font;
        label.textColor = color;
        label.textAlignment = align;
        label.text = text;
        
        return label;
    }
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:封装的button和image的创建工具类

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