iOS開發(fā)中常用到的宏定義

1.屏幕寬高

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kScreenHeight [UIScreen mainScreen].bounds.size.height

2.手機(jī)型號(hào)

#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)

3.適配相關(guān)

#define kScale_W(w) ((kScreenWidth)/375) * (w)//6為標(biāo)準(zhǔn)適配的,如果需要其他標(biāo)準(zhǔn)可以修改

#define kScale_H(h) (kScreenHeight/667) * (h)

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height//狀態(tài)欄高度

#define kStatusBarHeight (kISiPhoneX?44:20)//狀態(tài)欄高度

#define kTabBarHeight (StatusBarHeight > 20 ? 83 : 49)//標(biāo)簽欄高度

#define kNavBarHeight (StatusBarHeight + 44)//導(dǎo)航欄高度

#define kSafeAreaBottom (kISiPhoneX ? 34 : 0)//安全區(qū)高度

4.字體樣式

#define kBoldFont(x) [UIFont boldSystemFontOfSize:x]

#define kFont(x) [UIFont systemFontOfSize:x]

#define kNameFont(x) [UIFont fontWithName:@"Heiti SC"size:x]

5.顏色設(shè)置

#define kRGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]//RGB格式

#define kRGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]//RGBA格式

#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]//隨機(jī)顏色

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue &0xFF0000) >>16))/255.0green:((float)((rgbValue &0xFF00) >>8))/255.0blue:((float)(rgbValue &0xFF))/255.0alpha:1.0]//16進(jìn)制顏色

6.系統(tǒng)對(duì)象

#define kApplication [UIApplication sharedApplication]//APP對(duì)象 (單例對(duì)象)

#define kKeyWindow [UIApplication sharedApplication].keyWindow//主窗口 (keyWindow)

#define kUserDefaults [NSUserDefaults standardUserDefaults]//NSUserDefaults實(shí)例化

#define kNotificationCenter [NSNotificationCenter defaultCenter]//通知中心 (單例對(duì)象)

#define KPostNotification(name,obj,info) [[NSNotificationCenter defaultCenter]postNotificationName:name object:obj userInfo:info]//發(fā)送通知

#define kVersion [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"]//APP版本號(hào)

#define kSystemVersion [[UIDevice currentDevice] systemVersion]//系統(tǒng)版本號(hào)

7.簡單調(diào)用

#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//強(qiáng)引用

#define kLoadNib(nibName) [UINib nibWithNibName:nibName bundle:[NSBundle mainBundle]]//加載xib

#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

8. 獲取時(shí)間

#define kCurrentYear [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]//獲得當(dāng)前的年份

#define kCurrentMonth [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]//獲得當(dāng)前的月份

#define kCurrentDay [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]//獲得當(dāng)前的日期

#define kCurrentHour [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]//獲得當(dāng)前的小時(shí)

#define kCurrentMin [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]//獲得當(dāng)前的分

#define kCurrentSec [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]//獲得當(dāng)前的秒

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容