首页 > 代码库 > iOS重用宏定义

iOS重用宏定义

iOS 多快好省的宏(转)

//  字符串:
#ifndef nilToEmpty
#define nilToEmpty(object) (object!=nil)?object:@""
#endif

#ifndef formatStringOfObject
#define formatStringOfObject(object) [NSString stringWithFormat:@"%@", object]
#endif

#ifndef nilToEmptyFormatStringOfObject
#define nilToEmptyFormatStringOfObject(object) formatStringOfObject(nilToEmpty(object))
#endif



//  图片:
#ifndef imagePath
#define imagePath(imageName) [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]
#endif


//  颜色
#define RGBA(r, g, b, a)                    [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r, g, b)                        RGBA(r, g, b, 1.0f)
#define HEXCOLOR(c)                         [UIColor colorWithRed:((c>>16)&0xFF)/255.0f green:((c>>8)&0xFF)/255.0f blue:(c&0xFF)/255.0f alpha:1.0f];


//  debug
#define debug(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])


//  iOS 支持
#define SUPPORT_IPHONE_OS_VERSION(version) ( __IPHONE_OS_VERSION_MIN_REQUIRED <= version && __IPHONE_OS_VERSION_MAX_ALLOWED >= version)


//  Application delegate
#define ApplicationDelegate                 ((AppDelegate *)[[UIApplication sharedApplication] delegate])


//  主要单例
#define UserDefaults                        [NSUserDefaults standardUserDefaults]
#define NotificationCenter                  [NSNotificationCenter defaultCenter]
#define SharedApplication                   [UIApplication sharedApplication]


#define Bundle                              [NSBundle mainBundle]

#define MainScreen                          [UIScreen mainScreen]


//  网络指示
#define ShowNetworkActivityIndicator()      [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define HideNetworkActivityIndicator()      [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
#define NetworkActivityIndicatorVisible(x)  [UIApplication sharedApplication].networkActivityIndicatorVisible = x


//  主要控件
#define NavBar                              self.navigationController.navigationBar
#define TabBar                              self.tabBarController.tabBar


//  大小尺寸
#define ScreenWidth                         [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight                        [[UIScreen mainScreen] bounds].size.height

#define NavBarHeight                        self.navigationController.navigationBar.bounds.size.height
#define TabBarHeight                        self.tabBarController.tabBar.bounds.size.height


#define TouchHeightDefault                  44.0f
#define TouchHeightSmall                    32.0f


#define ViewWidth(v)                        v.frame.size.width
#define ViewHeight(v)                       v.frame.size.height
#define ViewX(v)                            v.frame.origin.x
#define ViewY(v)                            v.frame.origin.y


#define SelfViewWidth                       self.view.bounds.size.width
#define SelfViewHeight                      self.view.bounds.size.height


#define RectX(rect)                            rect.origin.x
#define RectY(rect)                            rect.origin.y
#define RectWidth(rect)                        rect.size.width
#define RectHeight(rect)                       rect.size.height


#define RectSetWidth(rect, w)                  CGRectMake(RectX(rect), RectY(rect), w, RectHeight(rect))
#define RectSetHeight(rect, h)                 CGRectMake(RectX(rect), RectY(rect), RectWidth(rect), h)
#define RectSetX(rect, x)                      CGRectMake(x, RectY(rect), RectWidth(rect), RectHeight(rect))
#define RectSetY(rect, y)                      CGRectMake(RectX(rect), y, RectWidth(rect), RectHeight(rect))


#define RectSetSize(rect, w, h)                CGRectMake(RectX(rect), RectY(rect), w, h)
#define RectSetOrigin(rect, x, y)              CGRectMake(x, y, RectWidth(rect), RectHeight(rect))



//  内存管理
#if ! __has_feature(objc_arc)
    #define SBAutorelease(__v) ([__v autorelease]);
    #define SBReturnAutoreleased SBAutorelease

    #define SBRetain(__v) ([__v retain]);
    #define SBReturnRetained SBRetain

    #define SBRelease(__v) ([__v release]);

    #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
#else
    // -fobjc-arc
    #define SBAutorelease(__v)
    #define SBReturnAutoreleased(__v) (__v)

    #define SBRetain(__v)
    #define SBReturnRetained(__v) (__v)

    #define SBRelease(__v)

    #if TARGET_OS_IPHONE
        // Compiling for iOS
        #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
            // iOS 6.0 or later
            #define SBDispatchQueueRelease(__v)
        #else
            // iOS 5.X or earlier
            #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
        #endif
    #else
        // Compiling for Mac OS X
        #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
            // Mac OS X 10.8 or later
            #define SBDispatchQueueRelease(__v)
        #else
            // Mac OS X 10.7 or earlier
            #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
        #endif
    #endif
#endif

  

iOS开发中那些高效常用的宏(转)

原文地址:http://blog.csdn.net/duxinfeng2010/article/details/9067947

//补充1

//----------------------图片----------------------------  
  
//读取本地图片  
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]  
  
//定义UIImage对象  
#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  
  
//定义UIImage对象  
#define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]]  
  
//建议使用前两种宏定义,性能高于后者  
//----------------------图片----------------------------  
//补充2

//----------------------其他----------------------------  
  
//方正黑体简体字体定义  
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]  
  
  
//定义一个API  
#define APIURL                @"http://xxxxx/"  
//登陆API  
#define APILogin              [APIURL stringByAppendingString:@"Login"]  
  
//设置View的tag属性  
#define VIEWWITHTAG(_OBJECT, _TAG)    [_OBJECT viewWithTag : _TAG]  
//程序的本地化,引用国际化的文件  
#define MyLocal(x, ...) NSLocalizedString(x, nil)  
  
//G-C-D  
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)  
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)  
  
//NSUserDefaults 实例化  
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]  
  
  
//由角度获取弧度 有弧度获取角度  
#define degreesToRadian(x) (M_PI * (x) / 180.0)  
#define radianToDegrees(radian) (radian*180.0)/(M_PI)  
  
  
  
//单例化一个类  
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \  
\  
static classname *shared##classname = nil; \  
\  
+ (classname *)shared##classname \  
{ \  
@synchronized(self) \  
{ \  
if (shared##classname == nil) \  
{ \  
shared##classname = [[self alloc] init]; \  
} \  
} \  
\  
return shared##classname; \  
} \  
\  
+ (id)allocWithZone:(NSZone *)zone \  
{ \  
@synchronized(self) \  
{ \  
if (shared##classname == nil) \  
{ \  
shared##classname = [super allocWithZone:zone]; \  
return shared##classname; \  
} \  
} \  
\  
return nil; \  
} \  
\  
- (id)copyWithZone:(NSZone *)zone \  
{ \  
return self; \  
}  
  
  
  
#endif
//补充3

//----------------------颜色类---------------------------  
// rgb颜色转换(16进制->10进制)  
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]  
  
//带有RGBA的颜色设置  
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]  
  
// 获取RGB颜色  
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]  
#define RGB(r,g,b) RGBA(r,g,b,1.0f)  
  
//背景色  
#define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]  
  
//清除背景色  
#define CLEARCOLOR [UIColor clearColor]  
  
#pragma mark - color functions  
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]  
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]  
  
//----------------------颜色类-------------------------- 
//补充4

//----------------------系统----------------------------  
  
//获取系统版本  
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]  
  
//获取当前语言  
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])  
  
//判断是否 Retina屏、设备是否%fhone 5、是否是iPad  
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)  
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)  
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  
  
  
//判断是真机还是模拟器  
#if TARGET_OS_IPHONE  
//iPhone Device  
#endif  
  
#if TARGET_IPHONE_SIMULATOR  
//iPhone Simulator  
#endif  
  
//检查系统版本  
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)  
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)  
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)  
  
  
//----------------------系统----------------------------