iOS 常用宏定義-通用適配

狀態(tài)欄高度

宏定義

常用定義

//加載圖片
#define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

//字體
#define FONT_SIZE(f) [UIFont systemFontOfSize:(f)]//不加粗
#define FONT_B_SIZE(f) [UIFont boldSystemFontOfSize:(f)]//加粗
#define NameFont(x) [UIFont fontWithName:@"PingFangSC-Light"size:x]//蘋方細(xì)體
#define NameBFont(x) [UIFont fontWithName:@"PingFangSC-Regular"size:x]//蘋方體
#define NumFont(x) [UIFont fontWithName:@"Helvetica Neue"size:x]//數(shù)字字體
#define CustomFont(name,x) [UIFont fontWithName:name size:x]//自定義字體

//弱引用self(用于block塊中)
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

// alert
#define Alert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示"message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"知道了"otherButtonTitles:nil] show]

#pragma mark- View設(shè)置
// View 圓角和加邊框
#define ViewBorderRadius(view, Radius, Width, Color)\
\
[view.layer setCornerRadius:(Radius)];\
[view.layer setMasksToBounds:YES];\
[view.layer setBorderWidth:(Width)];\
[view.layer setBorderColor:[Color CGColor]]

// View 圓角
#define ViewRadius(view, Radius)\
\
[view.layer setCornerRadius:(Radius)];\
[view.ayer setMasksToBounds:YES]

// View 陰影
#define ViewShadow(view,color,size,alpha,radius)\
\
view.layer.shadowColor = color.CGColor;\
view.layer.shadowOffset = size;\
view.layer.shadowOpacity = alpha;\
view.layer.shadowRadius = radius

// View 邊框
#define ViewBorder(view, Color, Width)\
\
[view.layer setBorderWidth:(Width)];\
[view.layer setBorderColor:(Color).CGColor]

#pragma mark- 系統(tǒng)信息
// 當(dāng)前語言
#define CURRENTLANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0])

// app版本號(hào)
#define VERSION (NSString *)[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]

// app Build版本號(hào)
#define BUILD (NSString *)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

#pragma mark- 判空
//字符串是否為空
#define ISNullString(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] <1? YES : NO )

//數(shù)組是否為空
#define ISNullArray(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count ==0||[array isEqual:[NSNull null]])

//字典是否為空
#define ISNullDict(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys ==0|| [dic isEqual:[NSNull null]])

//是否是空對(duì)象
#define ISNullObject(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] ==0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] ==0))

#pragma mark- 日志
//自定義Log
#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(FORMAT, ...) nil
#endif

#pragma mark- 持久化存儲(chǔ)
//永久存儲(chǔ)對(duì)象
#define SetUserDefaults(object, key) \
({ \
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; \
[defaults setObject:object forKey:key]; \
[defaults synchronize]; \
})

//獲取對(duì)象
#define GetUserDefaults(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]

//刪除某一個(gè)對(duì)象
#define kRemoveUserDefaults(key) \
({ \
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; \
[defaults removeObjectForKey:_key]; \
[defaults synchronize]; \
})

//清除 NSUserDefaults 保存的所有數(shù)據(jù)
#define kRemoveAllUserDefaults  [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]

#pragma mark- 時(shí)間對(duì)象
// 秒數(shù)
#define Seconds(Days) (24.f * 60.f * 60.f * (Days))
// 毫秒數(shù)
#define Milliseconds(Days) (24.f * 60.f * 60.f * 1000.f * (Days))
//獲得當(dāng)前的年份
#define  CurrentYear [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]
//獲得當(dāng)前的月份
#define  CurrentMonth [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]
//獲得當(dāng)前的日期
#define  CurrentDay  [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]
//獲得當(dāng)前的小時(shí)
#define  CurrentHour [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
//獲得當(dāng)前的分
#define  CurrentMin [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
//獲得當(dāng)前的秒
#define  CurrentSec [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]

常用顏色

// 顏色(RGB)
#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

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

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

文件夾/文件路徑

//獲取沙盒 temp
#define PathTemp NSTemporaryDirectory()
//獲取沙盒 Document
#define PathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define PathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

//Document路徑下文件/文件夾
#define DocumentPath(res) [PathDocument stringByAppendingPathComponent:res]
//Cache路徑下文件/文件夾
#define CachePath(res) [PathCache stringByAppendingPathComponent:res]
//Temp路徑下文件/文件夾
#define TempPath(res) [PathTemp stringByAppendingPathComponent:res]

frame設(shè)置

//是否是手機(jī)
#define IS_iPhone ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

//是否是iPad
#define IS_iPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

//屏幕寬
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

//屏幕高
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

//屏幕frame
#define SCREEN_FRAME [UIScreen mainScreen].bounds

// 判斷iPhone4系列
#define IsiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPhone5系列
#define IsiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPhone6系列
#define IS_isiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iphone6+系列
#define IsiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPhoneX
#define IsIPhone_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPHoneXr | 11
#define IsIPhone_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPhoneXs | 11Pro
#define IsIPhone_Xs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
// 判斷iPhoneXs Max | 11ProMax
#define IsIPhone_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iPhone12_Mini
#define IsIPhone_iPhone12_Mini ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1080, 2340), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iPhone12 | 12Pro | 14
#define IsIPhone_iPhone12 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1170, 2532), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iPhone12 Pro Max | 14 Max
#define IsIPhone_iPhone12_ProMax ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1284, 2778), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iPhone14 pro
#define IsIPhone_iPhone14_Pro ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1179, 2556), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)
//判斷iPhone14 Pro Max
#define IsIPhone_iPhone14_ProMax ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1290, 2796), [[UIScreen mainScreen] currentMode].size) && !IsPad : NO)

//statusbar的高度
#define STATUS_HEIGHT \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 13.0, *)) {\
        CGFloat topHeight = [UIApplication sharedApplication].windows.firstObject.safeAreaInsets.top;\
        height = topHeight ? topHeight : 20.0;\
    }else {\
        height = [[UIApplication sharedApplication] statusBarFrame].size.height;\
    }\
    (height);\
})\

//底部安全高度
#define BOTTOM_HEIGHT \
({\
    CGFloat height = 0.0;\
    if (@available(iOS 13.0, *)) {\
        NSSet *set = [UIApplication sharedApplication].connectedScenes;\
        UIWindowScene *windowScene = [set anyObject];\
        UIWindow *window = windowScene.windows.firstObject;\
        height = window.safeAreaInsets.bottom;\
    } else if (@available(iOS 11.0, *)) {\
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;\
        height = window.safeAreaInsets.bottom;\
    }\
    (height);\
})\

//navigation高度 (帶statusbar)
#define NAVGATION_HEIGHT (STATUS_HEIGHT + 44)

// View 坐標(biāo)(x,y)和寬高(width,height)
#define X(v)                    (v).frame.origin.x
#define Y(v)                    (v).frame.origin.y
#define WIDTH(v)                (v).frame.size.width
#define HEIGHT(v)              (v).frame.size.height
#define MinX(v)                CGRectGetMinX((v).frame)
#define MinY(v)                CGRectGetMinY((v).frame)
#define MidX(v)                CGRectGetMidX((v).frame)
#define MidY(v)                CGRectGetMidY((v).frame)
#define MaxX(v)                CGRectGetMaxX((v).frame)
#define MaxY(v)                CGRectGetMaxY((v).frame)

GCD多線程像捶、父視圖獲取

//延遲執(zhí)行
CG_INLINE void loadTime(CGFloat time ,dispatch_block_t block) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}

//回到主線程
CG_INLINE void runMain(dispatch_block_t block){
    dispatch_async(dispatch_get_main_queue(), block);
}

//只運(yùn)行一次
CG_INLINE void runOnce(dispatch_block_t block){
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, block);
}

//開辟異步線程
CG_INLINE void runAsyn(dispatch_block_t block){
    dispatch_async(dispatch_get_global_queue(0, 0), block);
}

//獲取添加了當(dāng)前view視圖的控制器
CG_INLINE UIViewController *superViewController(UIView *view){
    UIResponder *next = [view nextResponder];
    do{
        if([next isKindOfClass:[UIViewController class]]) {
            return(UIViewController*)next;
        }next = [next nextResponder];
    }while(next != nil);
    return nil;
}

//獲取當(dāng)前window展示的控制器
CG_INLINE UIViewController *currentViewController(){
    //獲取當(dāng)前活動(dòng)窗口的根數(shù)圖
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        //根據(jù)不同的頁面切換方式瞎饲,逐步獲取最上層的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController *)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController *)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else {
            break;
        }
    }
    return vc;
}

歡迎補(bǔ)充?懿帧!7肆埂;邸亦渗!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末又兵,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子枫笛,更是在濱河造成了極大的恐慌吨灭,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刑巧,死亡現(xiàn)場(chǎng)離奇詭異喧兄,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)啊楚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門吠冤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人特幔,你說我怎么就攤上這事咨演。” “怎么了蚯斯?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵薄风,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我拍嵌,道長(zhǎng)遭赂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任横辆,我火速辦了婚禮撇他,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘狈蚤。我一直安慰自己困肩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布脆侮。 她就那樣靜靜地躺著锌畸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪靖避。 梳的紋絲不亂的頭發(fā)上潭枣,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音幻捏,去河邊找鬼盆犁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛篡九,可吹牛的內(nèi)容都是我干的谐岁。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼榛臼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼翰铡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起讽坏,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤锭魔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后路呜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迷捧,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年胀葱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了漠秋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抵屿,死狀恐怖庆锦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情轧葛,我是刑警寧澤搂抒,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布艇搀,位于F島的核電站,受9級(jí)特大地震影響求晶,放射性物質(zhì)發(fā)生泄漏焰雕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一芳杏、第九天 我趴在偏房一處隱蔽的房頂上張望矩屁。 院中可真熱鬧,春花似錦爵赵、人聲如沸吝秕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烁峭。三九已至,卻和暖如春氛悬,著一層夾襖步出監(jiān)牢的瞬間则剃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工如捅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棍现,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓镜遣,卻偏偏與公主長(zhǎng)得像己肮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子悲关,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • iOS開發(fā)過程中谎僻,使用的一些常用宏定義 字符串是否為空#define kStringIsEmpty(str) ([...
    goyohol閱讀 5,343評(píng)論 30 85
  • pch 文件路徑 build settings 搜索Prifixheader $(SRCROOT)/黃色的文件...
    星9星閱讀 193評(píng)論 0 0
  • 字符串是否為空 數(shù)組是否為空 字典是否為空 是否是空對(duì)象 獲取屏幕寬度與高度 ( " \ ":連接行標(biāo)志,連接上下...
    為什么劃船不靠槳閱讀 381評(píng)論 0 0
  • 0寓辱、上線被拒原因有哪些: 答:1艘绍、未實(shí)現(xiàn)的功能模塊不能顯示。2秫筏、不能出現(xiàn)關(guān)于測(cè)試的一些信息诱鞠。3、第三方授權(quán)使用这敬,未...
    白水灬煮一切閱讀 2,024評(píng)論 0 57
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 29,336評(píng)論 8 265