美文网首页
iOS宏定义

iOS宏定义

作者: 涐罙擁那嗰懂涐 | 来源:发表于2016-09-13 14:47 被阅读0次

1 weakself和strongself

#ifndef weakify

#if DEBUG

#ifhas_feature(objc_arc)

#define weakify(object) autoreleasepool{} __weak __typeof(object) weak####object = object;

#else

#define weakify(object) autoreleasepool{} __block __typeof_(object) block####object = object;

#endif

#else

#if __has_feature(objc_arc)

#define weakify(object) try{}@finally{} {} __weak __typeof_(object) weak####object = object;

#else

#define weakify(object) try{}@finally{} {} __block __typeof_(object) block##_##object = object;

#endif

#endif

#endif

#ifndef strongify

#if DEBUG

#ifhas_feature(objc_arc)

#define strongify(object) autoreleasepool{} __typeof(object) object = weak####object;

#else

#define strongify(object) autoreleasepool{} __typeof_(object) object = block####object;

#endif

#else

#if __has_feature(objc_arc)

#define strongify(object) try{}@finally{} __typeof_(object) object = weak####object;

#else

#define strongify(object) try{}@finally{} __typeof_(object) object = block##_##object;

#endif

#endif

#endif

2 获取设备大小

#define NavigationBar_HEIGHT 44

3 获取屏幕 宽度、高度

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

4 打印日志

#ifdef DEBUG

define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt),PRETTY_FUNCTION,LINE, ##VA_ARGS);

#else

define DLog(...)

#endif

5 重写NSLog,Debug模式下打印日志和当前行数

#if DEBUG

#define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n",FUNCTION,LINE, [NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);

#else

#define NSLog(FORMAT, ...) nil

#endif

6 是否是ipad

#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

7 获取系统版本

#define IOS_VERSION [[UIDevice currentDevice] systemVersion] floatValue]

#define CurrentSystemVersion [UIDevice currentDevice] systemVersion]

8 获取当前系统的语言

#define CurrentLanguage ([NSLocale preferredLanguages] objectAtIndex:0])

9 判断设备的操做系统是不是ios7

#define IOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]

10 判断是真机还是模拟器

#if TARGET_OS_IPHONE

//iPhone Device

#endif

#if TARGET_IPHONE_SIMULATOR

//iPhone Simulator

#endif

11 颜色值转换

// 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]

12 由角度获取弧度 有弧度获取角度

#define degreesToRadian(x) (M_PI * (x) / 180.0)

#define radianToDegrees(radian) (radian*180.0)/(M_PI)

13 单例

//单例化一个类

#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; \ } 14 沙盒目录文件 //获取temp #define kPathTemp NSTemporaryDirectory() //获取沙盒 Document #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] //获取沙盒 Cache #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] 15 设置 view 圆角和边框 #define LRViewBorderRadius(View, Radius, Width, Color)\

\

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

[View.layer setMasksToBounds:YES];\

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

[View.layer setBorderColor:[Color CGColor]]

16 .判断当前的iPhone设备/系统版本

//判断是否为iPhone

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

//判断是否为iPad

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

//判断是否为ipod

#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

// 判断是否为 iPhone 5SE

#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f

// 判断是否为iPhone 6/6s

#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f

// 判断是否为iPhone 6Plus/6sPlus

#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]

//判断 iOS 8 或更高的系统版本

#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))

#ifdef ITTDEBUG

#define ITTDPRINT(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

#else

#define ITTDPRINT(xx, ...)  ((void)0)

#endif

相关文章

  • IOS NSLog宏定义

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

  • iOS 宏定义

    初学ios不久时间,对ios的宏定义充满好奇,宏的各种高级用法让ios代码变得优雅,那么宏究竟是什么,让我来花时间...

  • iOS 宏定义

    debug时打印,下面一句是解决xcode8打印不全的问题; 屏幕宽高,或者比例 NSUserDefaults 存...

  • iOS 宏定义

    ///屏幕宽度 #define KSCREENWIDTH [UIScreen mainScreen].bounds...

  • iOS宏定义

    1 weakself和strongself #ifndef weakify #if DEBUG #ifhas_fe...

  • iOS宏定义

    1. 宏定义简介 宏定义是使用#define将某段代码、字符串等一串的文字,用一个宏来代替的一种预处理方式。 2....

  • iOS 宏定义

  • iOS-常用宏定义

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

  • iOS常用宏定义

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

  • iOS之宏定义

    iOS开发小技巧之--WeakSelf宏的进化 宏定义不是C语句,也无须使用分号结束 宏定义并不是变量,它甚至不是...

网友评论

      本文标题:iOS宏定义

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