iOS常用宏定義及工具類

下面列些自己常用的宏定義

//
//  Macro.h
//  HKBaseClasses
//
//  Created by hooyking on 2020/4/26.
//  Copyright ? 2020 hooyking. All rights reserved.
//

#ifndef Macro_h
#define Macro_h

#define kScreenW                        [[UIScreen mainScreen] bounds].size.width
#define kScreenH                        [[UIScreen mainScreen] bounds].size.height
#define kAutoW(a)                       kScreenW/((UIInterfaceOrientationIsPortrait(kAppInterfaceOrientation)) ? 375.0 : 667.0)*a
#define kAutoH(b)                       kScreenH/((UIInterfaceOrientationIsPortrait(kAppInterfaceOrientation)) ? 667.0 : 375.0)*b
#define kTabBarH                        self.tabBarController.tabBar.frame.size.height
#define kNavConH                        self.navigationController.navigationBar.frame.size.height

#define kStatusBarH \
\
^(){ \
    CGFloat statusBarH = 0; \
    if (@available(iOS 13.0, *)) { \
        UIWindowScene *windowScene = (UIWindowScene *)[UIApplication sharedApplication].connectedScenes.allObjects.firstObject; \
        statusBarH = windowScene.statusBarManager.statusBarFrame.size.height; \
    } else { \
       statusBarH = [[UIApplication sharedApplication] statusBarFrame].size.height; \
    } \
    return statusBarH; \
}()

#define kAppWindow \
\
^(){ \
    UIWindow *window = [UIApplication sharedApplication].delegate.window; \
    if (!window) { \
        if (@available(iOS 13.0, *)) { \
            NSArray *array =[[[UIApplication sharedApplication] connectedScenes] allObjects]; \
            UIWindowScene *windowScene = (UIWindowScene*)array[0]; \
            UIWindow *mainWindow = [windowScene valueForKeyPath:@"delegate.window"]; \
            if(mainWindow) { \
                window = mainWindow; \
            } else { \
                window = [UIApplication sharedApplication].windows.lastObject; \
            } \
        } else { \
            window = [UIApplication sharedApplication].keyWindow; \
        } \
    } \
    return window; \
}()

//屏幕方向
#define kAppInterfaceOrientation \
\
^(){ \
    UIInterfaceOrientation interfaceOrientation; \
    if (@available(iOS 13.0, *)) { \
        UIWindowScene *windowScene = (UIWindowScene *)[UIApplication sharedApplication].connectedScenes.allObjects.firstObject; \
        interfaceOrientation = windowScene.interfaceOrientation; \
    } else { \
        interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; \
    } \
    return interfaceOrientation; \
}()

#define kSecurityH                      (kSafeAreaSeries ? (UIInterfaceOrientationIsPortrait(kAppInterfaceOrientation) ? 34.0 : 21.0) : 0)//底部安全高度

#define kSafeAreaSeries \
\
^(){ \
    if (@available(iOS 11.0, *)) { \
        CGFloat bottomSafeInset = kAppWindow.safeAreaInsets.bottom; \
        if (bottomSafeInset == 34.0 || bottomSafeInset == 21.0) { \
            return YES; \
        } \
    } \
    return NO; \
}()

#define kBelowIPhone5Series             ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(640,1136), [[UIScreen mainScreen] currentMode].size) || CGSizeEqualToSize(CGSizeMake(640,960), [[UIScreen mainScreen] currentMode].size)) : NO)
#define kIPhone4Series                  ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(640,960), [[UIScreen mainScreen] currentMode].size)) : NO)
#define kIPhone5Series                  ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(640,1136), [[UIScreen mainScreen] currentMode].size)) : NO)
#define kIPhone6Series                  ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(750,1334), [[UIScreen mainScreen] currentMode].size)) : NO)

#define kRGBColorAlpha(a,b,c,d)         [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:d]
#define kRGBColor(a,b,c)                kRGBColorAlpha(a,b,c,1.0)
//16位顏色值掠哥,使用方式:UIColorFromHexString(0xffffff)
#define kUIColorFromHexString(hexValue)       [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue &0xFF00) >>8))/255.0 blue:((float)(hexValue &0xFF))/255.0 alpha:1.0]
#define kRandomColor                    kRGBColor(arc4random() % 256, arc4random() % 256, arc4random() % 256)  //隨機(jī)色
//UIColor轉(zhuǎn)UIImage
#define kCreateImageForColor(color) \
\
^(){ \
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); \
    UIGraphicsBeginImageContext(rect.size); \
    CGContextRef context = UIGraphicsGetCurrentContext(); \
    CGContextSetFillColorWithColor(context, [color CGColor]); \
    CGContextFillRect(context, rect); \
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); \
    UIGraphicsEndImageContext(); \
    return theImage; \
}()

//字號(hào)
#define kFontWeight(size,fontWeight) \
\
^(){ \
    CGFloat fontSize = size; \
    if (kBelowIPhone5Series) { \
        fontSize --; \
    } else { \
        fontSize = size; \
    } \
    return [UIFont systemFontOfSize:fontSize weight:fontWeight]; \
}()

#define kFontSize(size) \
\
^(){ \
    CGFloat fontSize = size; \
    if (kBelowIPhone5Series) { \
        fontSize --; \
    } else { \
        fontSize = size; \
    } \
    return fontSize; \
}()

#define kFontHeavy(size)                kFontWeight(size,UIFontWeightHeavy)
#define kFontBold(size)                 kFontWeight(size,UIFontWeightBold)
#define kFontRegular(size)              kFontWeight(size,UIFontWeightRegular)
#define kFontMedium(size)               kFontWeight(size,UIFontWeightMedium)
#define kFontThin(size)                 kFontWeight(size,UIFontWeightThin)

//拼接字符串
#define kNSStringFormat(format,...)     [NSString stringWithFormat:format,##__VA_ARGS__]
//檢測(cè)是否含有目標(biāo)子串
#define kHasString(string,subString)    ([string rangeOfString:subString].location != NSNotFound)

#ifdef DEBUG
#define HKLog(...)                      printf("[%s] %s [第%d行]: %s\n", __TIME__ ,__PRETTY_FUNCTION__ ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define HKLog(...)
#endif

//強(qiáng)弱引用
#define kWeakSelf(type)                 __weak typeof(type) weak##type = type;
#define kStrongSelf(type)               __strong typeof(type) type = weak##type;

//當(dāng)前系統(tǒng)版本
#define kCurrentSystemVersion           [[UIDevice currentDevice].systemVersion doubleValue]
//當(dāng)前語(yǔ)言
#define kCurrentSystemLanguage          [[NSLocale preferredLanguages] objectAtIndex:0]
//app版本
#define kCurrentAppVersion              [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
//app名稱
#define kCurrentAppName                 [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]

//沙盒目錄路徑
#define kHomePath                       NSHomeDirectory()
#define kTemPath                        NSTemporaryDirectory()
#define kCachePath                      NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject
#define kLibraryPath                    NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject
#define kDocumentPath                   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject

//判斷是否為iPhone
#define IS_IPHONE                       ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
//判斷是否為iPad
#define IS_IPAD                         ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
//判斷是否為iPod
#define IS_IPOD                         ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

//打印方法運(yùn)行時(shí)間
#define METHOD_TIME_BEGIN               NSDate * startTime = [NSDate date];
#define METHOD_TIME_END                 HKLog(@"time interval: %f", -[startTime timeIntervalSinceNow]);

//監(jiān)聽通知
#define kNotifyAdd(noParamsFunc, notifyName)  [[NSNotificationCenter defaultCenter] \
addObserver:self \
selector:@selector(noParamsFunc) \
name:notifyName \
object:nil];
//發(fā)送通知
#define kNotifyPost(notifyName,obj)     [[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:obj];
//移除通知
#define kNotifyRemove(notifyName)       [[NSNotificationCenter defaultCenter] removeObserver:self name:notifyName object:nil];

#endif /* Macro_h */

再附上平時(shí)自己稍微整理用得上的工具類BaseClasses;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市缰犁,隨后出現(xiàn)的幾起案子济蝉,更是在濱河造成了極大的恐慌赔退,老刑警劉巖譬嚣,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蜻韭,居然都是意外死亡得院,警方通過查閱死者的電腦和手機(jī)傻铣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)祥绞,“玉大人非洲,你說(shuō)我怎么就攤上這事鸭限。” “怎么了两踏?”我有些...
    開封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵败京,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我梦染,道長(zhǎng)赡麦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任帕识,我火速辦了婚禮泛粹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肮疗。我一直安慰自己晶姊,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開白布伪货。 她就那樣靜靜地躺著们衙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪碱呼。 梳的紋絲不亂的頭發(fā)上蒙挑,一...
    開封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音巍举,去河邊找鬼。 笑死凝垛,一個(gè)胖子當(dāng)著我的面吹牛懊悯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播梦皮,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼炭分,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了剑肯?” 一聲冷哼從身側(cè)響起捧毛,我...
    開封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎让网,沒想到半個(gè)月后呀忧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡溃睹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年而账,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片因篇。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡泞辐,死狀恐怖笔横,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情咐吼,我是刑警寧澤吹缔,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站锯茄,受9級(jí)特大地震影響厢塘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撇吞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一俗冻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧牍颈,春花似錦迄薄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至画机,卻和暖如春冶伞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背步氏。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工响禽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荚醒。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓芋类,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親界阁。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侯繁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

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