ios中NSObject分类

时间:2023-03-10 02:15:08
ios中NSObject分类
#import <Foundation/Foundation.h>

#define BUNDLE_PATH_IMAGENAME(c) [[NSBundle mainBundle] pathForResource:c ofType:nil]

@interface NSObject (UICateGory)

@end

@interface UILabel (ext)

+(UILabel*)LabWithFrame:(CGRect)_rect text:(NSString*)aText textColor:(UIColor*)aColor textAlign:(NSTextAlignment)aAlign font:(UIFont*)aFont;

@end

#pragma mark ********** UIButton *************

@interface UIButton (ext)

+ (UIButton *)ButtonWithImageName:(NSString*)aImageName hImageName:(NSString*)aHImageName frame:(CGRect)aFrame title:(NSString *)aTitle titleColor:(UIColor *)aColor font:(UIFont *)aFont target:(id)aTarget action:(SEL)aAction;

+ (UIButton *)ButtonWithSystemImage:(CGRect)aFrame title:(NSString *)aTitle titleColor:(UIColor *)aColor font:(UIFont *)aFont target:(id)aTarget action:(SEL)aAction;
@end #pragma mark ********** UIImageView *************
@interface UIImageView (ext) + (UIImageView *)ImageViewImageName:(NSString*)aImageName frame:(CGRect)aRect; @end #pragma mark ********** UITableView *************
@interface UITableView (ext) + (UITableView *)TableViewWithFrame:(CGRect)frame style:(UITableViewStyle)style backgroundColor:(UIColor *)backgroundColor delegate:(id)delegate separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle; @end #pragma mark ********** UITextField ************* @interface UITextField (ext) +(UITextField*)TextFieldWithFrame:(CGRect)_rect target:(id)target textColor:(UIColor*)aTextColor textAlign:(NSTextAlignment)aAlign placeHolder:(NSString*)holder clearMode:(UITextFieldViewMode)aViewMode returnKey:(UIReturnKeyType)returnKeyType keyBord:(UIKeyboardType)keyBord; @end #pragma mark ********** UIImage ************* @interface UIImage (ext) + (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame;
@end =============== #import "NSObject+UICateGory.h" @implementation NSObject (UICateGory) @end @implementation UILabel (ext) +(UILabel*)LabWithFrame:(CGRect)_rect text:(NSString*)aText textColor:(UIColor*)aColor textAlign:(NSTextAlignment)aAlign font:(UIFont*)aFont{
UILabel *lab = [[[UILabel alloc] initWithFrame:_rect] autorelease];
lab.backgroundColor = [UIColor clearColor];
if ([aText length] > )
lab.text = aText;
if (aColor)
lab.textColor = aColor;
if(aAlign)
lab.textAlignment = aAlign;
if (aFont)
lab.font = aFont;
return lab;
}
@end @implementation UIButton (ext) + (UIButton *)ButtonWithImageName:(NSString*)aImageName hImageName:(NSString*)aHImageName frame:(CGRect)aFrame title:(NSString *)aTitle titleColor:(UIColor *)aColor font:(UIFont *)aFont target:(id)aTarget action:(SEL)aAction{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = aFrame;
if ([aImageName length] > ) {
UIImage *bgImg = [UIImage imageWithContentsOfFile:BUNDLE_PATH_IMAGENAME(aImageName)];
if ([bgImg respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
[button setBackgroundImage:[bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height/, bgImg.size.width/, bgImg.size.height/, bgImg.size.width/)] forState:UIControlStateNormal];
}else {
[button setBackgroundImage:[bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/ topCapHeight:bgImg.size.height/] forState:UIControlStateNormal];
}
}
if ([aHImageName length] > )
{
UIImage *bgImg = [UIImage imageWithContentsOfFile:BUNDLE_PATH_IMAGENAME(aHImageName)]; if ([bgImg respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
[button setBackgroundImage:[bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height/, bgImg.size.width/, bgImg.size.height/, bgImg.size.width/)] forState:UIControlStateHighlighted];
}else {
[button setBackgroundImage:[bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/ topCapHeight:bgImg.size.height/] forState:UIControlStateHighlighted];
}
}
if ([aTitle length] > )
[button setTitle:aTitle forState:UIControlStateNormal];
if (aColor)
[button setTitleColor:aColor forState:UIControlStateNormal];
if (aFont)
button.titleLabel.font = aFont;
[button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside]; return button;
}
+ (UIButton *)ButtonWithSystemImage:(CGRect)aFrame title:(NSString *)aTitle titleColor:(UIColor *)aColor font:(UIFont *)aFont target:(id)aTarget action:(SEL)aAction{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = aFrame;
if ([aTitle length] > )
[button setTitle:aTitle forState:UIControlStateNormal];
if (aColor)
[button setTitleColor:aColor forState:UIControlStateNormal];
if (aFont)
button.titleLabel.font = aFont;
[button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside]; return button;
} @end @implementation UIImageView (ext) + (UIImageView *)ImageViewImageName:(NSString*)aImageName frame:(CGRect)aRect{
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:aRect] autorelease];
imageView.userInteractionEnabled = YES;
UIImage *aImage = [UIImage imageWithContentsOfFile:BUNDLE_PATH_IMAGENAME(aImageName)];
if ([aImage respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
imageView.image = [aImage resizableImageWithCapInsets:UIEdgeInsetsMake(aImage.size.height/, aImage.size.width/, aImage.size.height/, aImage.size.width/)];
} else {
imageView.image = [aImage stretchableImageWithLeftCapWidth:aImage.size.width/ topCapHeight:aImage.size.height/];
}
return imageView;
} @end @implementation UITableView (ext) + (UITableView *)TableViewWithFrame:(CGRect)frame style:(UITableViewStyle)style backgroundColor:(UIColor *)backgroundColor delegate:(id)delegate separatorStyle:(UITableViewCellSeparatorStyle)separatorStyle { UITableView *tableView = [[[UITableView alloc] initWithFrame:frame style:style] autorelease];
[tableView setBackgroundColor:backgroundColor];
[tableView setDelegate:delegate];
[tableView setDataSource:delegate];
[tableView setSeparatorStyle:separatorStyle]; return tableView;
} @end @implementation UITextField (ext) +(UITextField*)TextFieldWithFrame:(CGRect)_rect target:(id)target textColor:(UIColor*)aTextColor textAlign:(NSTextAlignment)aAlign placeHolder:(NSString*)holder clearMode:(UITextFieldViewMode)aViewMode returnKey:(UIReturnKeyType)returnKeyType keyBord:(UIKeyboardType)keyBord{
UITextField *textField = [[[UITextField alloc] initWithFrame:_rect] autorelease];
textField.backgroundColor = [UIColor clearColor];
textField.delegate = target;
textField.returnKeyType = returnKeyType;
textField.keyboardType = keyBord;
if (aAlign)
textField.textAlignment = aAlign;
if (aTextColor)
textField.textColor = aTextColor;
if (aViewMode)
textField.clearButtonMode = aViewMode;
if ([holder length] > )
textField.placeholder = holder;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; return textField;
} @end #pragma mark ********** UIImage ************* @implementation UIImage (ext) + (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame
{
aFrame = CGRectMake(, , aFrame.size.width, aFrame.size.height);
UIGraphicsBeginImageContext(aFrame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, aFrame); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
@end