iOS常用的宏定义

1.尺寸,屏幕的宽高

#define JK_WIDTH  [UIScreen mainScreen].bounds.size.width
#define JK_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define JKSizeScale ((CIO_SCREEN_HEIGHT > 667) ? CIO_SCREEN_HEIGHT/667 : 1)

2.颜色

透明色
#define kClearColor [UIColor clearColor]
颜色的RGB
#define JKRGBColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
随机色
#define JKRandomColor  [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0]
带透明度的RGB
#define JKRGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
rgb颜色转换(16进制->10进制)
#define JKColorFromRGB(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]

3.字体的大小 font

#define  Font_text_12 [UIFont systemFontOfSize:12]
#define  Font_text_14 [UIFont systemFontOfSize:14]
#define  Font_text_16 [UIFont systemFontOfSize:16]

4. 开发输出模式的设置

#ifdef DEBUG   //处于开发阶段
#define JKNSLog(...) NSLog(__VA_ARGS__)
#else          //处于发布状态(没有输出)
#define  JKNSLog(...)
#endif

5.NSUserDefaults 的存取值

//获得存储的对象
#define UserDefaultObject(A) [[NSUserDefaults standardUserDefaults]objectForKey:A]
//存值(可变的值不可以存)
#define UserDefaultSetValue(B,C) [[NSUserDefaults standardUserDefaults]setObject:B forKey:C]
//存BOOL值
#define UserDefaultBool(D,E)  [[NSUserDefaults standardUserDefaults]setBool:D forKey:E]
#define  Synchronize          [[NSUserDefaults standardUserDefaults]synchronize]
清除存储对象
#define UserDefaultRemoveObjectForKey(__KEY__) \
{\
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
    [[NSUserDefaults standardUserDefaults] synchronize];\
}

6. 获取系统版本

#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])

7.获取当前的语言

#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])

8.通过路径加载本地的图片

#define JKLoadImage(filename,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]

9.在沙盒创建文件

 #define JKPLIST_TICKET_INFO_EDIT [NSHomeDirectory() stringByAppendingString:@"/Documents/data.plist"] //edit the plist

10.GCD

#define GCDWithGlobal(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define GCDWithMain(block) dispatch_async(dispatch_get_main_queue(),block)

11.快速查询一段代码的执行时间

/*     用法
       TICK
       do your work here
       TOCK
 */

#define TICK NSDate *startTime = [NSDate date];
#define TOCK NSLog(@"Time:%f", -[startTime timeIntervalSinceNow]);

12.机型的判断

/* 判断是否为iPhone */
   #define isiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

/* 判断是否是iPad */
   #define isiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

/* 判断是否为iPod */
   #define isiPod ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

13.由角度转换弧度 由弧度转换角度

 degree角度
 radian  弧度
 角度转弧度
 #define LRDegreesToRadian(angle)  (M_PI * (angle) / 180.0)
 弧度转角度
 #define LRRadianToDegrees(radian) (radian*180.0)/(M_PI)

14.弱引用

 #define JKWeakSelf __weak typeof(self) weakSelf = self;

15.打印函数,可以打印所在的函数,行数,以及你要打印的值

#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
NSLog(@"==1==%s 2==[Line %d] 3==%@", __PRETTY_FUNCTION__, __LINE__,string);

16.设置 view 圆角和边框

#define LRViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

17.宏定义一个弹窗方法,括号里面是方法的参数

#define JKShowAlert(message)    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: @"OK"];[alert show];

18.为空的判断

字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
是否是空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

19.APP版本号

#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

20.系统版本号

#define kSystemVersion [[UIDevice currentDevice] systemVersion]

21.获取t路径

获取沙盒Document路径
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
获取沙盒temp路径
#define kTempPath NSTemporaryDirectory()
获取沙盒Cache路径
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

22.获取一段时间间隔

#define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();

#define kEndTime  NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

23.方正黑体简体字体定义

 #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]

24.程序的本地化,引用国际化的文件

#define MyLocal(x, ...) NSLocalizedString(x, nil)

25.单例化一个类

#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

26.判断是真机还是模拟器

\#if TARGET_OS_IPHONE
//真机  
\#endif 
\#if TARGET_IPHONE_SIMULATOR
//模拟器
\#endif

27.在OC文件导入某些头文件

#ifdef __OBJC__
 //导入头文件
#endif

28.首次启动判断

#define First_Launched @"firstLaunch"

29.GCD 的宏定义

//GCD - 一次性执行

#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

//GCD - 在Main线程上运行

#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

//GCD - 开启异步线程

#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

推荐几篇博客

iOS中关于宏定义与常量的使用

你可能感兴趣的:(iOS常用的宏定义)