iOS 开发中规范的宏定义,基本常用的宏定义整理集锦。分享给大家!

  1 #ifndef MacroDefinition_h
  2 #define MacroDefinition_h
  3 //AppDelegate
  4  
  5 #define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication]  delegate]
  6 //----------------------系统设备相关----------------------------
  7 //获取设备屏幕尺寸
  8 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  9 #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//应用尺寸
 10 #define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
 11 #define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
 12 //获取系统版本
 13 #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
 14 #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
 15 #define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)
 16 #define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)
 17 #define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)
 18 #define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
 19 #define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
 20 #define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
 21 //获取当前语言
 22 #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
 23  
 24 //判断是否 Retina屏、设备是否%fhone 5、是否是iPad
 25 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
 26 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
 27 #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 28 //判断是真机还是模拟器
 29 #if TARGET_OS_IPHONE
 30 //iPhone Device
 31 #endif
 32 #if TARGET_IPHONE_SIMULATOR
 33 //iPhone Simulator
 34 #endif
 35 //----------------------系统设备相关----------------------------
 36  
 37 //----------------------内存相关----------------------------
 38 //使用ARC和不使用ARC
 39 #if __has_feature(objc_arc)
 40 //compiling with ARC
 41 #else
 42 // compiling without ARC
 43 #endif
 44 //释放一个对象
 45 #define SAFE_DELETE(P) if(P) { [P release], P = nil; }
 46 #define SAFE_RELEASE(x) [x release];x=nil
 47 //----------------------内存相关----------------------------
 48  
 49 //----------------------图片相关----------------------------
 50 //读取本地图片
 51 #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
 52 //定义UIImage对象
 53 #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
 54 //定义UIImage对象
 55 #define ImageNamed(_pointer) [UIImage imageNamed:_pointer]
 56 //可拉伸的图片
 57 #define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
 58 #define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
 59 //建议使用前两种宏定义,性能高于后者
 60 //----------------------图片相关----------------------------
 61  
 62 //----------------------颜色相关---------------------------
 63 // rgb颜色转换(16进制->10进制)
 64 #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]
 65 // 获取RGB颜色
 66 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
 67 #define RGB(r,g,b) RGBA(r,g,b,1.0f)
 68 //背景色
 69 #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
 70 //清除背景色
 71 #define CLEARCOLOR [UIColor clearColor]
 72 //----------------------颜色相关--------------------------
 73  
 74 //----------------------其他----------------------------
 75 //方正黑体简体字体定义
 76 #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
 77 //file
 78 //读取文件的文本内容,默认编码为UTF-8
 79 #define FileString(name,ext)            [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
 80 #define FileDictionary(name,ext)        [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
 81 #define FileArray(name,ext)             [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
 82 //G-C-D
 83 #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
 84 #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
 85 //Alert
 86 #define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
 87  
 88 //由角度获取弧度 有弧度获取角度
 89 #define degreesToRadian(x) (M_PI * (x) / 180.0)
 90 #define radianToDegrees(radian) (radian*180.0)/(M_PI)
 91 //----------------------其他-------------------------------
 92  
 93 //----------------------视图相关----------------------------
 94 //设置需要粘贴的文字或图片
 95 #define PasteString(string)   [[UIPasteboard generalPasteboard] setString:string];
 96 #define PasteImage(image)     [[UIPasteboard generalPasteboard] setImage:image];
 97  
 98 //得到视图的left top的X,Y坐标点
 99 #define VIEW_TX(view) (view.frame.origin.x)
100 #define VIEW_TY(view) (view.frame.origin.y)
101  
102 //得到视图的right bottom的X,Y坐标点
103 #define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
104 #define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
105  
106 //得到视图的尺寸:宽度、高度
107 #define VIEW_W(view)  (view.frame.size.width)
108 #define VIEW_H(view)  (view.frame.size.height)
109 //得到frame的X,Y坐标点
110 #define FRAME_TX(frame)  (frame.origin.x)
111 #define FRAME_TY(frame)  (frame.origin.y)
112 //得到frame的宽度、高度
113 #define FRAME_W(frame)  (frame.size.width)
114 #define FRAME_H(frame)  (frame.size.height)
115 //----------------------视图相关----------------------------
116  
117 //---------------------打印日志--------------------------
118 //Debug模式下打印日志,当前行,函数名
119 #if DEBUG
120 #define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
121 #else
122 #define NSLog(FORMAT, ...) nil
123 #endif
124 //Debug模式下打印日志,当前行,函数名 并弹出一个警告
125 #ifdef DEBUG
126 #   define  WDLog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
127 #else
128 #   define NSLog(...)
129 #endif
130 //打印Frame
131 #define LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
132 //打印Point
133 #define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)
134 //---------------------打印日志--------------------------
135 #endif

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。