美文网首页
个人使用的iOS开发宏定义

个人使用的iOS开发宏定义

作者: yunyang088 | 来源:发表于2016-04-22 16:19 被阅读269次

都是项目中常用的,16进制色值转换,默认字体设置字号大小,判断主线程,获取当前Version,Build,沙盒路径,缓存路径,屏幕物理宽、高,基于iPhone5尺寸的缩放值,当前手机版本,weakself等等。

  • 2015.04.27 更新
    新增比较当前系统版本号与输入值(NSString)
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

需要的就直接拷走好啦。

/*--------------------------------开发中常用到的宏定义-----------------------------------*/

// 颜色的转换
#define RGBCOLOR(r,g,b)             [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a)          [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

//16进制色值
#define HexRGB(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]

#define HexRGBAlpha(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]

//设置系统默认字体的字号
#define FONT(x)             [UIFont systemFontOfSize:x]

// 判断是否在主线程
#define DONT_BLOCK_UI() \\
NSAssert(![NSThread isMainThread], @"Don't block the UI thread please!")

#define BLOCK_UI() \\
NSAssert([NSThread isMainThread], @"You aren't running in the UI thread!")

//导航条高度
#define CUSTOM_NAV_HEIGHT            44
//当前build号
#define Current_Build               [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
#define Current_Version             [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

//常用系统库
#define USER_DEFAULT                [NSUserDefaults standardUserDefaults]
#define NOTIFICATION_CENTER         [NSNotificationCenter defaultCenter]

//沙盒存储路径
#define HomeDirectory               [NSHomeDirectory() stringByAppendingString:@"/Documents/"]

//缓存路径
#define APP_CACHES_PATH             [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]

//屏幕物理宽、高
#define APP_SCREEN_WIDTH            [UIScreen mainScreen].bounds.size.width
#define APP_SCREEN_HEIGHT           [UIScreen mainScreen].bounds.size.height

//与iPhone5比较的尺寸缩放值
#define kScaleFrom_iPhone5_Desgin(_X_) (_X_ * (APP_SCREEN_WIDTH/320))

//手机版本
#define Current_device_vesion       [[[UIDevice currentDevice] systemVersion] floatValue]

//按屏幕尺寸确定手机
#define iPhone5                     ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

//比较当前系统版本号与输入值(NSString)
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

//weakself 用与防止循环引用
#define WS(weakSelf)                __weak __typeof(&*self)weakSelf = self;

以后遇到新的好用的宏定义也会添加到这里,谢谢您的阅读。

相关文章

  • 个人使用的iOS开发宏定义

    都是项目中常用的,16进制色值转换,默认字体设置字号大小,判断主线程,获取当前Version,Build,沙盒路径...

  • iOS 常用宏定义

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

  • iOS之宏定义

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

  • iOS开发中常用的宏定义

    iOS开发中常用的宏定义 尺寸宏 打印宏 替换NSLog来使用,debug模式下可以打印很多方法名,行信息。 #...

  • iOS开发 宏定义

    在iOS开发中,合理的使用宏定义可以使我们少些好多代码 C预定义宏DATE //进行预处理的日期(“Mmm dd...

  • iOS 发布的Release版去掉NSLog打印

    我们利用宏定义可以方便我们的开发,在xcode中在哪里设置宏呢?如图所示: 在开发iOS应用时经常使用NSLog打...

  • ios拓展31-宏(define)和const的正确姿势

    在iOS开发中,经常用到宏定义,但是网上看一些博客的时候,有的说宏定义大量使用会重复拷贝,占用内存. 有的说不会...

  • Swift 声明宏定义

    在iOS开发中,使用Object-C可以定义一些宏,以方便项目中调用,但是Swift语言中,并不支持宏定义,我们可...

  • 宏 - 从入门到精通

    如果仅仅会使用类似上面的宏定义,那么其实对于宏定义的掌握只能算是刚刚入门,作为一个资深iOS开发者,非常有必要深入...

  • iOS宏关闭NSLog输出

    Release版本关闭Log输出 输出更详细的调试信息 参考文章:iOS开发-使用宏自定义输出(NSLog)

网友评论

      本文标题:个人使用的iOS开发宏定义

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