美文网首页
iOS 常用宏定义

iOS 常用宏定义

作者: 逆光少年 | 来源:发表于2016-12-24 16:58 被阅读43次
1.获取屏幕宽度与高度(支持横屏)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size)
#else
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif
2.设置RGB颜色/设置RGBA颜色
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0]
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
3.设置随机颜色
#define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
4.获取通知中心
#define XDNotificationCenter [NSNotificationCenter defaultCenter]
5.弱引用/强引用
#define XDWeakSelf(type) __weak typeof(type) weak##type = type;
#define XDStrongSelf(type) __strong typeof(type) type = weak##type;
6.获取沙盒目录文件
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
7.自定义日志
#ifdef DEBUG
#define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)

#endif
8.判断当前的iPhone设备/系统版本
//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])

//判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])

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

// 判断是否为 iPhone 5S/SE
#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f

// 判断是否为iPhone 6/6s/7
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f

// 判断是否为iPhone 6Plus/6sPlus/7p
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f

//获取系统版本(这个方法不是特别靠谱)
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

//建议使用这个方法
#define IOS_SYSTEM_STRING [[UIDevice currentDevice] systemVersion]

//判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
9.判断是真机还是模拟器
// 判断是不是iOS系统,如果是iOS系统在真机和模拟器输出都是YES
#if TARGET_OS_IPHONE #endif 

#if (TARGET_IPHONE_SIMULATOR)  
// 在模拟器的情况下
#else
 // 在真机情况下
#endif

相关文章

  • iOS常用宏定义

    打印日志的几种写法 推荐文章iOS 日常工作之常用宏定义大全iOS常用宏定义 结束语 到这里就结束了,如若不懂的话...

  • iOS-常用宏定义

    [转自:iOS常用宏定义][http://www.cocoachina.com/ios/20161207/1831...

  • iOS 常用宏定义

    iOS 开发中使用一些常用宏定义可以大大提高开发效率,提高代码的重用性.以下是一些常用的宏定义: 像这些宏定义,在...

  • IOS NSLog宏定义

    IOS NSLog宏定义 标签(空格分隔): IOS IOS NSLog宏定义 宏定义NSLog方法,不用加";"...

  • iOS常用宏定义

    1.UI元素 //NavBar高度#defineNAVIGATIONBAR_HEIGHT 44//StatusBa...

  • iOS常用宏定义

    字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[...

  • iOS常用宏定义

    字符串是否为空 数组是否为空 字典是否为空 是否是空对象 获取屏幕宽度与高度 ( " \ ":连接行标志,连接上下...

  • iOS常用宏定义

  • iOS常用宏定义

    #ifndef MacroDefinition_h #define MacroDefinition_h //---...

  • iOS常用宏定义

    整理 //常用宏定义 //是否为V以上系统 #define IOS(V) [[[UIDevice currentD...

网友评论

      本文标题:iOS 常用宏定义

      本文链接:https://www.haomeiwen.com/subject/imalvttx.html