iOS之常用宏定义

  1 下面我为大家提供一些常用的宏定义!
  2 
  3  将这些宏定义 加入到.pch使用 再也不用 用一次写一次这么长的程序了
  4 
  5 //-------------------获取设备大小-------------------------
  6 
  7 //NavBar高度
  8 
  9 #define NavigationBar_HEIGHT 44
 10 
 11 //获取屏幕 宽度、高度
 12 
 13 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
 14 
 15 #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
 16 
 17  
 18 
 19 //-------------------获取设备大小-------------------------
 20 
 21  
 22 
 23  
 24 
 25 //-------------------打印日志-------------------------
 26 
 27 //DEBUG  模式下打印日志,当前行
 28 
 29 #ifdef DEBUG
 30 
 31 #   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
 32 
 33 #else
 34 
 35 #   define DLog(...)
 36 
 37 #endif
 38 
 39  
 40 
 41  
 42 
 43 //重写NSLog,Debug模式下打印日志和当前行数
 44 
 45 #if DEBUG
 46 
 47 #define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
 48 
 49 #else
 50 
 51 #define NSLog(FORMAT, ...) nil
 52 
 53 #endif
 54 
 55  
 56 
 57 //DEBUG  模式下打印日志,当前行 并弹出一个警告
 58 
 59 #ifdef DEBUG
 60 
 61 #   define ULog(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]; }
 62 
 63 #else
 64 
 65 #   define ULog(...)
 66 
 67 #endif
 68 
 69  
 70 
 71  
 72 
 73 #define ITTDEBUG
 74 
 75 #define ITTLOGLEVEL_INFO     10
 76 
 77 #define ITTLOGLEVEL_WARNING  3
 78 
 79 #define ITTLOGLEVEL_ERROR    1
 80 
 81  
 82 
 83 #ifndef ITTMAXLOGLEVEL
 84 
 85  
 86 
 87 #ifdef DEBUG
 88 
 89 #define ITTMAXLOGLEVEL ITTLOGLEVEL_INFO
 90 
 91 #else
 92 
 93 #define ITTMAXLOGLEVEL ITTLOGLEVEL_ERROR
 94 
 95 #endif
 96 
 97  
 98 
 99 #endif
100 
101  
102 
103 // The general purpose logger. This ignores logging levels.
104 
105 #ifdef ITTDEBUG
106 
107 #define ITTDPRINT(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
108 
109 #else
110 
111 #define ITTDPRINT(xx, ...)  ((void)0)
112 
113 #endif
114 
115  
116 
117 // Prints the current method's name.
118 
119 #define ITTDPRINTMETHODNAME() ITTDPRINT(@"%s", __PRETTY_FUNCTION__)
120 
121  
122 
123 // Log-level based logging macros.
124 
125 #if ITTLOGLEVEL_ERROR <= ITTMAXLOGLEVEL
126 
127 #define ITTDERROR(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
128 
129 #else
130 
131 #define ITTDERROR(xx, ...)  ((void)0)
132 
133 #endif
134 
135  
136 
137 #if ITTLOGLEVEL_WARNING <= ITTMAXLOGLEVEL
138 
139 #define ITTDWARNING(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
140 
141 #else
142 
143 #define ITTDWARNING(xx, ...)  ((void)0)
144 
145 #endif
146 
147  
148 
149 #if ITTLOGLEVEL_INFO <= ITTMAXLOGLEVEL
150 
151 #define ITTDINFO(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
152 
153 #else
154 
155 #define ITTDINFO(xx, ...)  ((void)0)
156 
157 #endif
158 
159  
160 
161 #ifdef ITTDEBUG
162 
163 #define ITTDCONDITIONLOG(condition, xx, ...) { if ((condition)) { \
164 
165 ITTDPRINT(xx, ##__VA_ARGS__); \
166 
167 } \
168 
169 } ((void)0)
170 
171 #else
172 
173 #define ITTDCONDITIONLOG(condition, xx, ...) ((void)0)
174 
175 #endif
176 
177  
178 
179 #define ITTAssert(condition, ...)                                       \
180 
181 do {                                                                      \
182 
183     if (!(condition)) {                                                     \
184 
185         [[NSAssertionHandler currentHandler]                                  \
186 
187          handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
188 
189          file:[NSString stringWithUTF8String:__FILE__]  \
190 
191          lineNumber:__LINE__                                  \
192 
193          description:__VA_ARGS__];                             \
194 
195     }                                                                       \
196 
197 } while(0)
198 
199  
200 
201 //---------------------打印日志--------------------------
202 
203  
204 
205  
206 
207 //----------------------系统----------------------------
208 
209  
210 
211 //获取系统版本
212 
213 #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
214 
215 #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
216 
217  
218 
219 //获取当前语言
220 
221 #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
222 
223  
224 
225 //判断是否 Retina屏、设备是否%fhone 5、是否是iPad
226 
227 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
228 
229 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
230 
231 #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
232 
233  
234 
235  
236 
237 //判断是真机还是模拟器
238 
239 #if TARGET_OS_IPHONE
240 
241 //iPhone Device
242 
243 #endif
244 
245  
246 
247 #if TARGET_IPHONE_SIMULATOR
248 
249 //iPhone Simulator
250 
251 #endif
252 
253  
254 
255 //检查系统版本
256 
257 #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
258 
259 #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
260 
261 #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
262 
263 #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
264 
265 #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
266 
267  
268 
269  
270 
271 //----------------------系统----------------------------
272 
273  
274 
275  
276 
277 //----------------------内存----------------------------
278 
279  
280 
281 //使用ARC和不使用ARC
282 
283 #if __has_feature(objc_arc)
284 
285 //compiling with ARC
286 
287 #else
288 
289 // compiling without ARC
290 
291 #endif
292 
293  
294 
295 #pragma mark - common functions
296 
297 #define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
298 
299  
300 
301 //释放一个对象
302 
303 #define SAFE_DELETE(P) if(P) { [P release], P = nil; }
304 
305  
306 
307 #define SAFE_RELEASE(x) [x release];x=nil
308 
309  
310 
311  
312 
313  
314 
315 //----------------------内存----------------------------
316 
317  
318 
319  
320 
321 //----------------------图片----------------------------
322 
323  
324 
325 //读取本地图片
326 
327 #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
328 
329  
330 
331 //定义UIImage对象
332 
333 #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
334 
335  
336 
337 //定义UIImage对象
338 
339 #define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]]
340 
341  
342 
343 //建议使用前两种宏定义,性能高于后者
344 
345 //----------------------图片----------------------------
346 
347  
348 
349  
350 
351  
352 
353 //----------------------颜色类---------------------------
354 
355 // rgb颜色转换(16进制->10进制)
356 
357 #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]
358 
359  
360 
361 //带有RGBA的颜色设置
362 
363 #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
364 
365  
366 
367 // 获取RGB颜色
368 
369 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
370 
371 #define RGB(r,g,b) RGBA(r,g,b,1.0f)
372 
373  
374 
375 //背景色
376 
377 #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
378 
379  
380 
381 //清除背景色
382 
383 #define CLEARCOLOR [UIColor clearColor]
384 
385  
386 
387 #pragma mark - color functions
388 
389 #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
390 
391 #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
392 
393  
394 
395 //----------------------颜色类--------------------------
396 
397  
398 
399  
400 
401  
402 
403 //----------------------其他----------------------------
404 
405  
406 
407 //方正黑体简体字体定义
408 
409 #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
410 
411  
412 
413  
414 
415 //定义一个API
416 
417 #define APIURL                @"http://xxxxx/"
418 
419 //登陆API
420 
421 #define APILogin              [APIURL stringByAppendingString:@"Login"]
422 
423  
424 
425 //设置View的tag属性
426 
427 #define VIEWWITHTAG(_OBJECT, _TAG)    [_OBJECT viewWithTag : _TAG]
428 
429 //程序的本地化,引用国际化的文件
430 
431 #define MyLocal(x, ...) NSLocalizedString(x, nil)
432 
433  
434 
435 //G-C-D
436 
437 #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
438 
439 #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
440 
441  
442 
443 //NSUserDefaults 实例化
444 
445 #define USER_DEFAULT [NSUserDefaults standardUserDefaults]
446 
447  
448 
449  
450 
451 //由角度获取弧度 有弧度获取角度
452 
453 #define degreesToRadian(x) (M_PI * (x) / 180.0)
454 
455 #define radianToDegrees(radian) (radian*180.0)/(M_PI)
456 
457  
458 
459  
460 
461  
462 
463 //单例化一个类
464 
465 #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
466 
467 \
468 
469 static classname *shared##classname = nil; \
470 
471 \
472 
473 + (classname *)shared##classname \
474 
475 { \
476 
477     @synchronized(self) \
478 
479     { \
480 
481         if (shared##classname == nil) \
482 
483         { \
484 
485             shared##classname = [[self alloc] init]; \
486 
487         } \
488 
489     } \
490 
491     \
492 
493     return shared##classname; \
494 
495 } \
496 
497 \
498 
499 + (id)allocWithZone:(NSZone *)zone \
500 
501 { \
502 
503     @synchronized(self) \
504 
505     { \
506 
507         if (shared##classname == nil) \
508 
509         { \
510 
511             shared##classname = [super allocWithZone:zone]; \
512 
513             return shared##classname; \
514 
515         } \
516 
517     } \
518 
519     \
520 
521     return nil; \
522 
523 } \
524 
525 \
526 
527 - (id)copyWithZone:(NSZone *)zone \
528 
529 { \  
530 
531     return self; \  
532 
533 }

 

posted @ 2016-03-10 15:14  Jaycee麦子  阅读(368)  评论(0编辑  收藏  举报