前言:最近忙里偷闲整理了下项目中常用的宏,以备以后不时之需。
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** 工具类 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
// 打印
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#define LogFrame(frame) NSLog(@"frame[X = %.1f,Y = %.1f,W = %.1f,H = %.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)
#define LogPoint(point) NSLog(@"Point[X = %.1f,Y = %.1f]",point.x,point.y)
#define LogMethod NSLog(@"[%s] %@", class_getName([self class]), NSStringFromSelector(_cmd));
#else
#define LRLog(...)
#endif
// 颜色相关
#define RGB_ALPHA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define RGB(r,g,b) RGB_ALPHA(r,g,b,1)
#define RANDOM arc4random_uniform(256)
#define RANDOM_COLOR RGB(RANDOM/255.0,RANDOM/255.0,RANDOM/255.0) // 获取随机color
#define IOS_VERSION ([[UIDevice currentDevice].systemVersion floatValue]) // 获取系统当前版本号
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) // 是否为iphone
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) // 判断是否为iPad
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") // 大于等于7.0的ios版本
#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") // 大于等于8.0的ios版本
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180) // 角度转弧度
#define CURRENT_LANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0]) // 获取当前系统语言
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self // 取self
#define keypath(objc,keypath) @(((void)objc.keypath,#keypath)) // 快速获取当前控制器属性
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** 尺寸相关 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
//获取屏幕尺寸 返回屏幕高度宽度
#define SCREEN_BOUNDS [[UIScreen mainScreen] bounds]
#define SCREEN_SIZE SCREEN_BOUNDS.size
#define SCREEN_HEIGHT SCREEN_SIZE.height // 获取屏幕的高度
#define SCREEN_WIDTH SCREEN_SIZE.width // 获取屏幕的宽度
// 判断屏幕尺寸 返回BOOL值
#define IPHONE3_5 (SCREEN_WIDTH == 320.0f && SCREEN_HEIGHT == 480.0f) // 判断屏幕3.5英寸
#define IPHONE4_0 (SCREEN_WIDTH == 320.0f && SCREEN_HEIGHT == 568.0f) // 判断屏幕4.0英寸
#define IPHONE4_7 (SCREEN_WIDTH == 375.0f && SCREEN_HEIGHT == 667.0f) // 判断屏幕4.7英寸
#define IPHONE5_3 (SCREEN_WIDTH == 414.0f && SCREEN_HEIGHT == 736.0f) // 判断屏幕5.5英寸
#define STATUS_BAR_HEIGHT 20 // 状态栏高度
#define NAVIGATION_BAR_HEIGHT 44 // NavBar高度
#define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT)) // 状态栏 + 导航栏 高度
#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale)) // 屏幕分辨率
#define SYRealValue(value) ((value)/414.0f*[UIScreen mainScreen].bounds.size.width) // 自动适配
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** about develop view ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
// 视图设置圆角
#define VIEW_RADIUS(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
#define GETNIB(para) [[NSBundle mainBundle]loadNibNamed:para owner:nil options:nil].firstObject // 获取nib文件
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** 沙盒目录文件 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
#define PATH_TEMP NSTemporaryDirectory() // 获取temp目录
#define PATH_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] // 获取document目录
#define PATH_CACHE [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] // 获取沙盒 Cache
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** 通知Notification相关的宏 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
#define TNCancelFavoriteProductNotification @"TNCancelFavoriteProductNotification" //取消收藏时
#define TNMarkFavoriteProductNotification @"TNMarkFavoriteProductNotification" //标记收藏时
#define kNotficationDownloadProgressChanged @"kNotficationDownloadProgressChanged" //下载进度变化
#define kNotificationPauseDownload @"kNotificationPauseDownload" //暂停下载
#define kNotificationStartDownload @"kNotificationStartDownload" //开始下载
#define kNotificationDownloadSuccess @"kNotificationDownloadSuccess" //下载成功
#define kNotificationDownloadFailed @"kNotificationDownloadFailed" //下载失败
#define kNotificationDownloadNewMagazine @"kNotificationDownloadNewMagazine"
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** 服务端API接口的宏 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
#ifdef DEBUG
#define API_BASE_URL_STRING @"http://boys.test.companydomain.com/api/"
#else
#define API_BASE_URL_STRING @"http://www.companydomain.com/api/"
#endif
//接口
#define GET_CONTENT_DETAIL @"channel/getContentDetail" //获取内容详情(含上一个和下一个)
#define GET_COMMENT_LIST @"comment/getCommentList" //获取评论列表
#define COMMENT_LOGIN @"comment/login" //获取评论列表
#define COMMENT_PUBLISH @"comment/publish" //发布评论
#define COMMENT_DELETE @"comment/delComment" //删除评论
#define LOGINOUT @"common/logout" //登出
/* ---------------------------------------------------------------------------------
** ** ** ** ** ** ** ** ** ** Fundation 工具类 ** ** ** ** ** ** ** ** ** **
---------------------------------------------------------------------------------
**/
// 转化
#define DATA_CONVERT_CHAR(ADATA) [[NSString alloc] initWithData:ADATA encoding:NSUTF8StringEncoding] // NSData-> NSString
#define CHAR_CONVERT_DATA(CHARS) [CHARS dataUsingEncoding: NSUTF8StringEncoding] // NSString->NSData
#define DATA_IMAGE(IMAGEDT) [UIImage imageWithData: IMAGEDT] // NSData->UIImage
#define IMAGE_DATA(IMAGE) UIImagePNGRepresentation(IMAGE) // UIImage -> NSData
总结:文章可能存在错别字或者理解不到位的地方,欢迎挑刺,如果你也是一枚IOS爱好者,请加入我们的QQ群:126440594。