iOS-日常開發常用巨集定義

一個孤獨的搬碼猿發表於2019-03-05

鎮樓專用
日常開發常用巨集定義

#pragma mark - 字型、顏色相關
#define kFONT_SIZE(f)            [UIFont systemFontOfSize:(f)]
#define kFONT_BOLD_SIZE(f)       [UIFont boldSystemFontOfSize:(f)]
#define kFONT_ITALIC_SIZE(f)     [UIFont italicSystemFontOfSize:(f)]
#define kRGBCOLOR(r,g,b)         [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]
#define kRGBACOLOR(r,g,b,a)      [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f 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]
#define kColorWithHex(rgbValue)  [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:1.0]
///=============================================================================

#pragma mark - 圖片載入
// 載入圖片
#define kGetImage(imageName)                        [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
// 讀取本地圖片 (檔名,字尾名)
#define kGetBundleImage(__FILENAME__,__EXTENSION__) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:__FILENAME__ ofType:__EXTENSION__]]
///=============================================================================

#pragma mark - 控制檯列印
#ifdef DEBUG
#define kLog(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 - 判斷資料是否為空
// 字串是否為空
#define kISNullString(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
// 陣列是否為空
#define kISNullArray(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0 ||[array isEqual:[NSNull null]])
// 字典是否為空
#define kISNullDict(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0 || [dic isEqual:[NSNull null]])
// 是否是空物件
#define kISNullObject(_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 - Application相關
// APP物件 (單例物件)
#define kApplication         [UIApplication sharedApplication]
// APP物件
#define kAppDelegate         (AppDelegate*)[[UIApplication sharedApplication] delegate]
// 主視窗 (keyWindow)
#define kKeyWindow           [UIApplication sharedApplication].keyWindow
// NSUserDefaults例項化
#define kUserDefaults        [NSUserDefaults standardUserDefaults]
// 通知中心 (單例物件)
#define kNotificationCenter  [NSNotificationCenter defaultCenter]
//獲取temp
#define kPathTemp            NSTemporaryDirectory()
//獲取沙盒 Document
#define kPathDocument        [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define kPathCache           [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
///=============================================================================

#pragma mark - 螢幕座標、尺寸相關
// 判斷是否iPhone X
#define kIS_iPhoneX             UIApplication.sharedApplication.statusBarFrame.size.height > 20 : YES : NO
// 螢幕高度
#define kScreenHeight           [[UIScreen mainScreen] bounds].size.height
// 螢幕寬度
#define kScreenWidth            [[UIScreen mainScreen] bounds].size.width
// 狀態列高度
#define kStatusBarHeight        UIApplication.sharedApplication.statusBarFrame.size.height
// 頂部導航欄高度
#define kNavigationBarHeight    44.f
// 狀態列高度 + 頂部導航欄高度
#define kSafeAreaTopHeight      UIApplication.sharedApplication.statusBarFrame.size.height + 44
// 底部安全距離
#define kSafeAreaBottomHeight   (IS_iPhoneX ? 34.f : 0.f)
// Tabbar高度
#define kTabbarHeight           49.f

// 控制元件尺寸比例
#define kScreenWidthRate        ([[UIScreen mainScreen] bounds].size.width/375.f)
// 實際寬尺寸
#define kSuitWidthSize(size)    kScreenWidthRate * (size)
// 控制元件尺寸比例
#define kScreenHeightRate       ([[UIScreen mainScreen] bounds].size.height/667.f)
// 實際高尺寸
#define kSuitHeightSize(size)   kScreenHeightRate * (size)
///=============================================================================

#pragma mark - 強弱引用
#define kWeakSelf(type)  __weak typeof(type) weak##type = type;
#define kStrongSelf(type)  __strong typeof(type) type = weak##type;
///=============================================================================
複製程式碼

相關文章