ios中NSObject分类

本文深入探讨了iOS开发领域的关键技术和实践,包括Objective-C和Swift语言的使用,Xcode开发环境,以及UIKit、CoreGraphics等核心框架的应用。同时,介绍了如何进行高效代码管理和优化性能的方法。
摘要由CSDN通过智能技术生成
#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] > 0)
        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] > 0) {
        UIImage *bgImg = [UIImage imageWithContentsOfFile:BUNDLE_PATH_IMAGENAME(aImageName)];
        if ([bgImg respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
            [button setBackgroundImage:[bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height/2, bgImg.size.width/2, bgImg.size.height/2, bgImg.size.width/2)] forState:UIControlStateNormal];
        }else {
            [button setBackgroundImage:[bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/2 topCapHeight:bgImg.size.height/2] forState:UIControlStateNormal];
        }
    }
    if ([aHImageName length] > 0)
    {
        UIImage *bgImg = [UIImage imageWithContentsOfFile:BUNDLE_PATH_IMAGENAME(aHImageName)];
        
        if ([bgImg respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
            [button setBackgroundImage:[bgImg resizableImageWithCapInsets:UIEdgeInsetsMake(bgImg.size.height/2, bgImg.size.width/2, bgImg.size.height/2, bgImg.size.width/2)] forState:UIControlStateHighlighted];
        }else {
            [button    setBackgroundImage:[bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/2 topCapHeight:bgImg.size.height/2] forState:UIControlStateHighlighted];
        }
    }
    if ([aTitle length] > 0)
        [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] > 0)
        [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/2, aImage.size.width/2, aImage.size.height/2, aImage.size.width/2)];
    } else {
        imageView.image = [aImage stretchableImageWithLeftCapWidth:aImage.size.width/2 topCapHeight:aImage.size.height/2];
    }
    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] > 0)
        textField.placeholder = holder;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    return textField;
}

@end

#pragma mark ********** UIImage *************

@implementation UIImage (ext)

+ (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame
{
    aFrame = CGRectMake(0, 0, 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值