欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 常用的宏定义

程序员文章站 2022-10-25 08:50:50
屏幕尺寸 #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height 手机型号 #define kISiPhon ......

屏幕尺寸

#define kscreenwidth [uiscreen mainscreen].bounds.size.width

#define kscreenheight [uiscreen mainscreen].bounds.size.height

手机型号

 #define kisiphone (ui_user_interface_idiom() == uiuserinterfaceidiomphone)

 #define kscreenmaxlength (max(kscreenwidth, kscreenheight))

 #define kscreenminlength (min(kscreenwidth, kscreenheight))

#define kisiphone5 (kisiphone && kscreenmaxlength == 568.0)

#define kisiphone6 (kisiphone && kscreenmaxlength == 667.0)
#define kisiphone6p (kisiphone && kscreenmaxlength == 736.0)
#define kisiphonex (kisiphone && kscreenmaxlength == 812.0)
#define kisiphonexr (kisiphone && kscreenmaxlength == 896.0)
#define kisiphonexx (kisiphone && kscreenmaxlength > 811.0)
#define ios8 ([[[uidevice currentdevice] systemversion] doublevalue] >= 8.0)


系统版本
#define ios810 ([[[uidevice currentdevice] systemversion] doublevalue] >= 10.0)
适配尺寸
//6为标准适配的,如果需要其他标准可以修改
#define kscale_w(w) ((kscreenwidth)/375) * (w)
#define kscale_h(h) (kscreenheight/667) * (h)
//状态栏高度
#define kstatusbarheight [[uiapplication sharedapplication] statusbarframe].size.height
//状态栏高度
#define statusbarheight (kisiphonex?44:20)
//标签栏高度
#define ktabbarheight (statusbarheight > 20 ? 83 : 49)
//导航栏高度
#define knavbarheight (statusbarheight + 44)
//安全区高度
#define ksafeareabottom (kisiphonex ? 34 : 0)

字体大小

#define kboldfont(x) [uifont boldsystemfontofsize:x]
#define kfont(x) [uifont systemfontofsize:x]
颜色设置
//rgb格式
#define krgb(r, g, b) [uicolor colorwithred:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
//rgba格式
#define krgba(r, g, b, a) [uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
//随机颜色
#define krandomcolor [uicolor colorwithred:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

系统相关
//app对象 (单例对象)
#define kapplication [uiapplication sharedapplication]
//主窗口 (keywindow)
#define kkeywindow [uiapplication sharedapplication].keywindow
//nsuserdefaults实例化
#define kuserdefaults [nsuserdefaults standarduserdefaults]
//通知中心 (单例对象)
#define knotificationcenter [nsnotificationcenter defaultcenter]
//发送通知
#define kpostnotification(name,obj,info) [[nsnotificationcenter defaultcenter]postnotificationname:name object:obj userinfo:info]
//app版本号
#define kversion [[[nsbundle mainbundle] infodictionary] valueforkey:@"cfbundleshortversionstring"]
//系统版本号
#define ksystemversion [[uidevice currentdevice] systemversion]


 常用设置

//加载图片
#define kgetimage(imagename) [uiimage imagenamed:[nsstring stringwithformat:@"%@",imagename]]
//弱引用
#define kweakself(type)  __weak typeof(type) weak##type = type
//强引用
#define kstrongself(type)  __strong typeof(type) type = weak##type
//安全调用block
#define ksafeblock(blockname,...) ({!blockname ? nil : blockname(__va_args__);})
//加载xib
#define kloadnib(nibname) [uinib nibwithnibname:nibname bundle:[nsbundle mainbundle]]
//字符串拼接
#define kstringformat(format,...) [nsstring stringwithformat:format,##__va_args__]
//属性快速声明(建议使用代码块)
#define kpropertystring(name) @property(nonatomic,copy)nsstring *name
#define kpropertystrong(type,name) @property(nonatomic,strong)type *name
#define kpropertyassign(name) @property(nonatomic,assign)nsinteger name
// view 圆角和加边框
#define kviewborderradius(view, radius, width, color)\
\
[view.layer setcornerradius:(radius)];\
[view.layer setmaskstobounds:yes];\
[view.layer setborderwidth:(width)];\
[view.layer setbordercolor:[color cgcolor]]
// view 圆角
#define kviewradius(view, radius)\
\
[view.layer setcornerradius:(radius)];\
[view.layer setmaskstobounds:yes]
//永久存储对象
#define ksetuserdefaults(object, key)                                                                                                 \
({                                                                                                                                             \
nsuserdefaults * defaults = [nsuserdefaults standarduserdefaults];                                                                         \
[defaults setobject:object forkey:key];                                                                                                    \
[defaults synchronize];                                                                                                                    \
})
//获取对象
#define kgetuserdefaults(key) [[nsuserdefaults standarduserdefaults] objectforkey:key]
//删除某一个对象
#define kremoveuserdefaults(key)                                         \
({                                                                          \
nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];       \
[defaults removeobjectforkey:_key];                                     \
[defaults synchronize];                                                 \
})
//清除 nsuserdefaults 保存的所有数据
#define kremovealluserdefaults  [[nsuserdefaults standarduserdefaults] removepersistentdomainforname:[[nsbundle mainbundle] bundleidentifier]]

日期时间
//获得当前的年份
#define  kcurrentyear [[nscalendar currentcalendar] component:nscalendarunityear fromdate:[nsdate date]]
//获得当前的月份
#define  kcurrentmonth [[nscalendar currentcalendar] component:nscalendarunitmonth fromdate:[nsdate date]]
//获得当前的日期
#define  kcurrentday  [[nscalendar currentcalendar] component:nscalendarunitday fromdate:[nsdate date]]
//获得当前的小时
#define  kcurrenthour [[nscalendar currentcalendar] component:nscalendarunithour fromdate:[nsdate date]]
//获得当前的分
#define  kcurrentmin [[nscalendar currentcalendar] component:nscalendarunitminute fromdate:[nsdate date]]
//获得当前的秒
#define  kcurrentsec [[nscalendar currentcalendar] component:nscalendarunitsecond fromdate:[nsdate date]]

沙河路径

//获取沙盒 temp
#define kpathtemp nstemporarydirectory()
//获取沙盒 document
#define kpathdocument [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) firstobject]
//获取沙盒 cache
#define kpathcache [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) firstobject]
//library/caches 文件路径
#define kfilepath ([[nsfilemanager defaultmanager] urlfordirectory:nscachesdirectory indomain:nsuserdomainmask appropriateforurl:nil create:yes error:nil])