众所周知,在iOS开发过程中我们可以使用一些常用的宏来提高开发效率和代码的重用性;将这些宏放到一个头文件里,然后再把这个头文件放到工程中的-Prefix.pch文件中,使用起来很方便。
下面就来分类说明这些常用的宏定义。
1. 打印日志相关:由于性能原因,我们不能在release的版本里面使用NSLog。
#ifdef __OPTIMIZE__
# define NSLog(...) {}
#else
# define NSLog(...) NSLog(__VA_ARGS__)
#endif
解释:
release模式通常会定义 __OPTIMIZE__,当然debug模式不会。将这段代码放在你的头文件当中,你就可以放心的使用NSLog了!
2. 获取一个应用的动态版本号
#define AppVersionShort [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
3. 应用运行的设备上面的系统号判断
//ios8 and later
#define IS_IOS8 ((floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1))
#define IS_IOS9 ((floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_3))
4. 定义一个字体
#define HelveticaNeue_Light(x) [UIFont fontWithName:@"HelveticaNeue-Light" size:x]
5.定义一个颜色
#define UIColorFromRGBWithAlpha(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]
这里的rgbValue是十六进制,比如0Xededed; a是一个float.
6. 屏幕的宽和高,这个如果手动来做layout的话很有用。
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
网友评论