常用宏匯總
#pragma mark - NSLog宏定義
#ifdef RELEASE
#define NSLog(...)
#else
// 使用UTF8String的原因就是printf是C語言的流济,所以需要通過這個方法轉(zhuǎn)換一下才能打印
#define FuncString [[NSString stringWithFormat:@"%s", __func__].lastPathComponent UTF8String]
#define NSLog(...) printf("[HB]: %s 第%d行 日志信息如下:\n%s\n\n", FuncString, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#endif
#pragma mark - 判斷對象是否為空
// 字符串是否為空
#define K_STRING_IS_EMPTY(str) (nil == str || [str isKindOfClass:[NSNull class]] || [str length] < 1)
// 數(shù)組是否為空
#define K_ARRAY_IS_EMPTY(array) (nil == array || [array isKindOfClass:[NSNull class]] || array.count == 0)
// 字典是否為空
#define K_DICT_IS_EMPTY(dict) (nil == dict || [dict isKindOfClass:[NSNull class]] || dict.allKeys.count == 0)
// 是否是空對象
#define K_OBJECT_IS_EMPTY(_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 - 屏幕尺寸
// 獲取屏幕寬度與高度崖咨,確保在iOS8之后不受橫豎屏的影響
// nativeBounds是屏幕像素盾戴,不隨橫豎平改變的
#define K_SCREEN_WIDTH \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
#define K_SCREEN_HEIGHT \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
#define K_SCREEN_BOUNDS (CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT))
#define K_SCREEN_SIZE (CGSizeMake(K_SCREEN_WIDTH, K_SCREEN_HEIGHT))
#pragma mark - 一些縮寫
#define K_APPLICATION [UIApplication sharedApplication]
#define K_KEY_WINDOW [UIApplication sharedApplication].keyWindow
#define K_APP_DELEGATE [UIApplication sharedApplication].delegate
#define K_USER_DEFAULTS [NSUserDefaults standardUserDefaults]
#define K_NOTIFICATION_CENTER [NSNotificationCenter defaultCenter]
#pragma mark - App相關(guān)
// App版本號
#define K_APP_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// App的唯一標(biāo)識
#define K_APP_BUNDLE_ID [[NSBundle mainBundle] bundleIdentifier]
#pragma mark - 真機(jī)相關(guān)
// 系統(tǒng)版本號
#define K_SYSTEM_VERSION [[UIDevice currentDevice] systemVersion]
// 獲取當(dāng)前語言
#define K_CURRENT_LANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0])
// 判斷是否為iPhone
#define K_IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
// 判斷是否為iPad
#define K_IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#pragma mark - 沙盒路徑
// Document路徑
#define K_DOCUMENT_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// temp路徑
#define K_TEMP_PATH NSTemporaryDirectory()
// Cache路徑
#define K_CACHE_PATH [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#pragma mark - Color
#define K_RGB_COLOR(r, g, b) \
[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define K_RGBA_COLOR(r, g, b, a) \
[UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
// 使用方法:K_COLOR_WITH_HEX(0xFFFFFF)
#define K_COLOR_WITH_HEX(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]
// 使用方法:K_COLOR_WITH_HEX_ALPHA(0xFFFFFF, 0.6)
#define K_COLOR_WITH_HEX_ALPHA(hexValue, a) \
[UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:a]
#pragma mark - 其他
// 弱引用
#define K_WEAK_SELF(type) __weak typeof(type) weak##type = type;
// 強(qiáng)引用
#define K_STRONG_SELF(type) __strong typeof(type) type = weak##type;
// 由角度轉(zhuǎn)換弧度
#define K_DEGREES_TO_RADIAN(x) (M_PI * (x) / 180.0)
// 由弧度轉(zhuǎn)換角度
#define K_RADIAN_TO_DEGREES(radian) (radian * 180.0) / (M_PI)
// 記錄時間間隔
#define K_START_TIME CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define K_END_TIME NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
// iOS11適配,解決在沒有導(dǎo)航欄的情況下下拉刷新按鈕暴露在外部的問題
#define K_ADJUSTS_SCROLLVIEW_INSETS_NO(scrollView, vc)\
do {\
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"")\
if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
[scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\
} else {\
vc.automaticallyAdjustsScrollViewInsets = NO;\
}\
_Pragma("clang diagnostic pop")\
} while (0)
/** --------------------判斷是真機(jī)還是模擬器络断,可以用這段代碼來判斷真機(jī)或模擬器來執(zhí)行不同邏輯-------------------- */
#if TARGET_OS_IPHONE
// 真機(jī)
#endif
#if TARGET_IPHONE_SIMULATOR
// 模擬器
#endif
/** --------------------判斷是真機(jī)還是模擬器,可以用這段代碼來判斷真機(jī)或模擬器來執(zhí)行不同邏輯-------------------- */
解決iOS8后bounds
受橫豎屏
的影響
在***iOS7***中輸出:
Landscape: NO
UIScreen.mainScreen().bounds: (0.0, 0.0, 320.0, 568.0)
Landscape: YES
UIScreen.mainScreen().bounds: (0.0, 0.0, 320.0, 568.0)
-------------------------------------------------------------------------
在***iOS8***中輸出:
Landscape: NO
UIScreen.mainScreen().bounds: (0.0, 0.0, 320.0, 568.0)
Landscape: YES
UIScreen.mainScreen().bounds: (0.0, 0.0, 568.0, 320.0)
- 方案1(
推薦
)
- 在iOS8中增加了2個屬性项玛,通過這兩個屬性可以解決iOS8后的問題
- nativeBounds:屏幕
像素
貌笨,不隨橫豎平改變的
- nativeScale:
1(non retina) / 2(retina) / 3(retina hd)
- 方案2
+ (CGSize)screenSize {
CGSize screenSize = [UIScreen mainScreen].bounds.size;
return CGSizeMake(MIN(screenSize.width, screenSize.height), MAX(screenSize.width, screenSize.height));
}
參考鏈接