常用宏汇总
#pragma mark - NSLog宏定义
#ifdef RELEASE
#define NSLog(...)
#else
// 使用UTF8String的原因就是printf是C语言的,所以需要通过这个方法转换一下才能打印
#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)
// 数组是否为空
#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相关
// App版本号
#define K_APP_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// App的唯一标识
#define K_APP_BUNDLE_ID [[NSBundle mainBundle] bundleIdentifier]
#pragma mark - 真机相关
// 系统版本号
#define K_SYSTEM_VERSION [[UIDevice currentDevice] systemVersion]
// 获取当前语言
#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;
// 强引用
#define K_STRONG_SELF(type) __strong typeof(type) type = weak##type;
// 由角度转换弧度
#define K_DEGREES_TO_RADIAN(x) (M_PI * (x) / 180.0)
// 由弧度转换角度
#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适配,解决在没有导航栏的情况下下拉刷新按钮暴露在外部的问题
#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)
/** --------------------判断是真机还是模拟器,可以用这段代码来判断真机或模拟器来执行不同逻辑-------------------- */
#if TARGET_OS_IPHONE
// 真机
#endif
#if TARGET_IPHONE_SIMULATOR
// 模拟器
#endif
/** --------------------判断是真机还是模拟器,可以用这段代码来判断真机或模拟器来执行不同逻辑-------------------- */
解决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));
}
参考链接