下面是我在开发中封装到的一些经常使用的一些方法,如有写错的地方或者大家有更好的方法,还请大家给我提出来,大家一起学习!谢谢!
1.常用控件方法的封装:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyUtil : NSObject
//创建标签的方法
+ (UILabel *)createLabelFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines;
+ (UILabel *)createLabelFrame:(CGRect)frame text:(NSString *)text color:(UIColor *)textColor;
//创建按钮的方法
+ (UIButton *)createBtnFrame:(CGRect)frame title:(NSString *)title bgImageName:(NSString *)bgImageName target:(id)target action:(SEL)action;
//创建图片视图的方法
+ (UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName;
//类型的英文转化成中文
+ (NSString *)transferCateName:(NSString *)name;
@end
方法的实现:
#import"MyUtil.h"
@implementation MyUtil
+(UILabel *)createLabelFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines
{
UILabel *label = [[UILabelalloc] initWithFrame:frame];
label.text = text;
label.textAlignment = textAlignment;
label.textColor = textColor;
label.numberOfLines = numberOfLines;
return label;
}
+(UILabel *)createLabelFrame:(CGRect)frame text:(NSString *)text color:(UIColor *)textColor
{
return [selfcreateLabelFrame:frame text:texttextColor:textColortextAlignment:NSTextAlignmentCenternumberOfLines:1];
}
+(UIButton *)createBtnFrame:(CGRect)frame title:(NSString *)title bgImageName:(NSString *)bgImageName target:(id)target action:(SEL)action
{
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btnsetTitle:title forState:UIControlStateNormal];
[btnsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[btnsetBackgroundImage:[UIImageimageNamed:bgImageName] forState:UIControlStateNormal];
[btnaddTarget:target action:actionforControlEvents:UIControlEventTouchUpInside];
return btn;
}
+(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName
{
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:frame];
imageView.image = [UIImageimageNamed:imageName];
return imageView;
}
+ (NSString *)transferCateName:(NSString *)name
{
if ([name isEqualToString:@"Business"]) {
return @"商业";
}elseif ([name isEqualToString:@"Weather"]) {
return @"天气";
}elseif ([name isEqualToString:@"Tool"]) {
return @"工具";
}elseif ([name isEqualToString:@"Travel"]) {
return @"旅行";
}elseif ([name isEqualToString:@"Sports"]) {
return @"体育";
}elseif ([name isEqualToString:@"Social"]) {
return @"社交";
}elseif ([name isEqualToString:@"Refer"]) {
return @"参考";
}elseif ([name isEqualToString:@"Ability"]) {
return @"效率";
}elseif ([name isEqualToString: