iOS - 常用宏定义和PCH文件知识点整理
程序员文章站
2022-03-22 14:49:27
(一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类、某个函数、某一行) (2)常用屏幕适配宏(方法有很多可以灵活运用) (3)其他宏运用补充: (三)后续常用宏定义会慢慢补充; ......
(一)pch文件操作步骤演示:
第一步:图文所示:
第二步:图文所示:
(二)常用宏定义整理:
(1)常用log日志宏(输出日志详细可定位某个类、某个函数、某一行)
//============================================================nslog #ifdef debug #define ddlog(fmt, ...) nslog((@"%s [line %d] " fmt), __pretty_function__, __line__, ##__va_args__) #define base_url @"" #else #define ddlog(...) #define base_url @"" #endif #ifdef debug #define ddstring [nsstring stringwithformat:@"%s", __file__].lastpathcomponent #define ddlog(...) printf("%s: %s [%d]: %s\n\n",[[nsstring dh_stringdate] utf8string], [ddstring utf8string] ,__line__, [[nsstring stringwithformat:__va_args__] utf8string]); #define base_url @"" #else #define ddlog(...) #define base_url @"" #endif //============================================================(第二个比较详细适应于真机模拟器)
#pragma mark - 其中第二个宏定义中会用到(nsstring分类文件,该文件如下) // nsstring+dhextension.h 文件中 #import <foundation/foundation.h> @interface nsstring (dhextension) /** log日志会用到 @return nsstring */ + (nsstring *)dh_stringdate; @end // nsstring+dhextension.m 文件中 #import "nsstring+dhextension.h" @implementation nsstring (dhextension) #pragma mark - log日志会用到 + (nsstring *)dh_stringdate { nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; nsstring *datestring = [dateformatter stringfromdate:[nsdate date]]; return datestring; } @end
(2)常用屏幕适配宏(方法有很多可以灵活运用)
//============================================================uiscreen/iphone机型判断 // 屏幕宽 #define ddscreenw [uiscreen mainscreen].bounds.size.width // 屏幕高 #define ddscreenh [uiscreen mainscreen].bounds.size.height // 用户机型判断宏 #define ddcurrentmodesize [[uiscreen mainscreen] currentmode].size // 判断是否是ipad #define ddipad ([[uidevice currentdevice] userinterfaceidiom] == uiuserinterfaceidiompad) // 判断iphone4系列 #define ddiphone4 ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(640, 960), ddcurrentmodesize) && !ddipad : no) // 判断iphone5系列 #define ddiphone5 ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(640, 1136), ddcurrentmodesize) && !ddipad : no) // 判断iphone6系列(包含iphone6/iphone7/iphone8) #define ddiphone6 ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(750, 1334), ddcurrentmodesize) && !ddipad : no) // 判断iphone6p系列(包含iphone6p/iphone7p/iphone8p) #define ddiphonep ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(1242, 2208), ddcurrentmodesize) && !ddipad : no) // 判断iphonex #define ddiphonex ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(1125, 2436), ddcurrentmodesize) && !ddipad : no) // 判断iphonexr #define ddiphonexr ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(828, 1792), ddcurrentmodesize) && !ddipad : no) // 判断iphonexs #define ddiphonexs ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(1125, 2436), ddcurrentmodesize) && !ddipad : no) // 判断iphonexs max #define ddiphonexsmax ([uiscreen instancesrespondtoselector:@selector(currentmode)] ? cgsizeequaltosize(cgsizemake(1242, 2688), ddcurrentmodesize) && !ddipad : no) // 顶部navbar高度 #define ddnavbarh ((ddiphonex==yes || ddiphonexr==yes || ddiphonexs==yes || ddiphonexsmax==yes) ? 88.0 : 64.0) // 底部tabbar高度 #define ddtabbarh ((ddiphonex==yes || ddiphonexr==yes || ddiphonexs==yes || ddiphonexsmax==yes) ? 83.0 : 49.0) // 顶部statusbar高度 #define ddstatusbarh ((ddiphonex==yes || ddiphonexr==yes || ddiphonexs==yes || ddiphonexsmax==yes) ? 44.0 : 20.0) // 判断是不是刘海屏(iphonex系列) #define ddiphonenewx ((ddnavbarh==88.0 || ddiphonexr==83.0 || ddstatusbarh==44.0) ? yes : no) // 底部安全区域高度 #define ddtabbarsafeh (ddiphonenew ? 34.f : 0.f) // 顶部适配系统方法 #define status_bar_frame [[uiapplication sharedapplication] statusbarframe] // iphonex系列判断(包含iphone x/iphone xs/iphone xs max/iphone xr) #define ddis_iphonex (cgsizeequaltosize(cgsizemake(375.f, 812.f), [uiscreen mainscreen].bounds.size) || cgsizeequaltosize(cgsizemake(812.f, 375.f), [uiscreen mainscreen].bounds.size) || cgsizeequaltosize(cgsizemake(414.f, 896.f), [uiscreen mainscreen].bounds.size) || cgsizeequaltosize(cgsizemake(896.f, 414.f), [uiscreen mainscreen].bounds.size)) //============================================================uiscreen/iphone机型判断(方法有很多种灵活运用)
(3)其他宏运用补充:
//============================================================rgb #define ddrgbcolor(r, g, b) [uicolor colorwithred:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define ddbackgroundrgb ddrgbcolor(236, 236, 236) #define uicolorfromhex(s) [uicolor colorwithred:(((s & 0xff0000) >> 16))/255.0green:(((s &0xff00) >>8))/255.0blue:((s &0xff))/255.0alpha:1.0] //============================================================默认图片 #define ddplaceimage [uiimage imagenamed:@"imagename.png"] //============================================================self #define weakself(weakself) __weak __typeof(self) weakself = self;
(三)后续常用宏定义会慢慢补充;