常用宏

一、iOS 中常用的宏

设置Dlog可以打印出类名,方法名,行数.

#ifdef DEBUG

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

#else

#define DLog(...)

#endif

尺寸宏

// 宽和高基本都是用的#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height// navigationBar 默认的高度#define NAVIGATIONBAR_HEIGHT64// tabBar 默认的高度#define UITABBAR_HEIGHT49

颜色宏

// RGB颜色转换(16进制->10进制)#define UICOLOR_HEX(hexString) [UIColor colorWithRed:((float)((rgbValue &0xFF0000) >>16))/255.0green:((float)((rgbValue &0xFF00) >>8))/255.0blue:((float)(rgbValue &0xFF))/255.0alpha:1.0]// 带有RGBA的颜色设置#define UICOLOR_RGB(R, G, B, A) [UIColor colorWithRed:R/255.0green:G/255.0blue:B/255.0alpha:A]// 随机颜色#define UICOLOR_RANDOM  [UIColor colorWithRed:(arc4random()%255)/255.0green:(arc4random()%255)/255.0blue:(arc4random()%255)/255.0alpha:1.0]

打印宏

// 直接替换#ifdefDEBUG#defineNSLog(...) NSLog(__VA_ARGS__)#definedebugMethod() NSLog(@"%s", __func__)#else#defineNSLog(...)#definedebugMethod()#endif

真机还是模拟器

#ifTARGET_OS_IPHONE//iPhone Device#endif#ifTARGET_IPHONE_SIMULATOR//iPhone Simulator#endif

系统宏

//获取系统版本#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]// 大于 iOS 8 的系统#define ABOVE_IOS8 (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0) ? YES : NO)// 直接判断机型#define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen]bounds].size.height- (double)480) < DBL_EPSILON)#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height- (double)568) < DBL_EPSILON)#define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height- (double)667) < DBL_EPSILON)#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height- (double)736) < DBL_EPSILON)

目录宏

// Directory 目录#definePATH_DIRECTORY [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]// Cache 目录#definePATH_CACHE [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]// 数据库 目录#definePATH_DATABASE_CACHE [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]

区分不同版本的文字宽度处理

#if__IPHONE_OS_VERSION_MIN_REQUIRED >=70000#defineMB_TEXTSIZE(text, font) [text length] >0? [text \sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero;#else#defineMB_TEXTSIZE(text, font) [text length] >0? [text sizeWithFont:font] : CGSizeZero;#endif#if__IPHONE_OS_VERSION_MIN_REQUIRED >=70000#defineMB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] >0? [text \boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \        attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;#else#defineMB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] >0? [text \sizeWithFont:fontconstrainedToSize:maxSize lineBreakMode:mode] : CGSizeZero;#endif

二、自带的宏

//获取两者之间小的#define MIN(A,B)    ((A) < (B) ? (A) : (B))//获取两者之间大的#define MAX(A,B)    ((A) > (B) ? (A) : (B))//绝对值#define ABS(A)    ((A) < 0 ? (-(A)) : (A))

//CGFLOAT_MAX 无穷大//CGFLOAT_MIN 无穷接近0#ifdefined(__LP64__) && __LP64__#defineCGFLOAT_TYPE double#defineCGFLOAT_IS_DOUBLE 1#defineCGFLOAT_MIN DBL_MIN#defineCGFLOAT_MAX DBL_MAX#else#defineCGFLOAT_TYPE float#defineCGFLOAT_IS_DOUBLE 0#defineCGFLOAT_MIN FLT_MIN#defineCGFLOAT_MAX FLT_MAX#endif

// 这三个倒是用的不多#defineNSIntegerMax    LONG_MAX#defineNSIntegerMin    LONG_MIN#defineNSUIntegerMax  ULONG_MAX

//  UIKIT_EXTERN     extern

#ifdef__cplusplus

#defineUIKIT_EXTERN        extern"C"__attribute__((visibility ("default")))

#else

#defineUIKIT_EXTERN            extern __attribute__((visibility ("default")))

#endif

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