.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
网友评论