iOS常用的宏定义(实用tips)

//
//  PrefixHeader.pch
//  DriveJouyney
//
//  Created by Chan on 2019/5/15.
//  Copyright © 2019 Chan. All rights reserved.
//

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

// MARK: - scrollViewInsets
#define  kAdjustsScrollViewInsets_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)

// MARK: - NSLOG
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LRLog(...)

#endif


// MARK: - common use
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]


#define kURLWithString(str)  [NSURL URLWithString:str]
#define kGetUserDefaults(key) [[NSUserDefaults standardUserDefaults] objectForKey:key]
#define kUserDefaults(key, value) [[NSUserDefaults standardUserDefaults] setObject:value forKey:key]
#define kSynchronize [[NSUserDefaults standardUserDefaults] synchronize]
#define kRemoveOUserDefaults(key) [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]
// iOS6-iOS9判断
#define ISIOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define ISIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define ISIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define ISIOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)


#define kIntToStr(intValue) ([NSString stringWithFormat:@"%@", @(intValue)])
#define kFloatToStr(floatValue) ([NSString stringWithFormat:@"%.2f", floatValue])
#define kNumberToStr(NumberValue) ([NSString stringWithFormat:@"%@", NumberValue])
#define kUint64ToStr(uint64) [@(uint64) stringValue]
#define kStrToInt(str) [str integerValue]
#define kStrToDouble(str) [str doubleValue]


// MARK: - setBorder&Radius
#define kViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]

// MARK: - loadNibView
#define kLoadNibView(className) [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(className) owner:self options:nil].lastObject

#define kAppWindow  [[[UIApplication sharedApplication] delegate] window]
#define AppVersion  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
#define AppName  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
#define kIOS11Later [[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0
#define kAdaptiveRate [UIScreen mainScreen].bounds.size.height / 736
#define kFontScale kScreenWidth/375
#define kFontSize(size) [UIFont systemFontOfSize:size*kFontScale]
#define kNavBarTintColor UIColorHex(F05B48)
#define kWhiteColor [UIColor whiteColor]

#define kImage(named) [UIImage imageNamed:named]

// MARK: - frame &adapter
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kRect(x,y,w,h) CGRectMake(x*kFontScale, y*kAdaptiveRate, w*kFontScale, h*kAdaptiveRate)
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define KNavBarHeight (kStatusBarHeight+44)
#define kDevice_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

#define kTabbarHeight      kDevice_Is_iPhoneX ?83: 49
#define kBodyHeight     (kScreenHeight -KNavBarHeight)
#define kMiddleHeight   (kBodyHeight -kTabbarHeight )
#define kSearchBarHeight    45
#define kNavigationHeight   44

// MARK: - Color
#define UIColorRGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
// 设置颜色 示例:UIColorHex(0x26A7E8)
#define UIColorHex(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]
// 设置颜色与透明度 示例:UIColorHEX_Alpha(0x26A7E8, 0.5)
#define UIColorHex_Alpha(rgbValue, al) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:al]

#pragma mark - CommonColor
// 导航栏背景颜色
#define kColorNavBground UIColorHex(0xf3ee64)
// 深灰色
#define kColorDarkgray UIColorHex(0x666666)
// 淡灰色-如普通界面的背景颜色
#define kColorLightgray UIColorHex(0xeeeeee)
// 灰色—如内容字体颜色
#define kColorgrayContent UIColorHex(0x969696)
// 搜索焦点颜色
#define kTintColorSearch UIColorRGB(2, 162, 253)
// 主题背景色
#define kBackgroundColor UIColorHex(0xf2f2f2)
// cell高亮颜色
#define kCellHightedColor UIColorHex(0xe6e6e9)
// 通用的红色文字颜色
#define kColorFontRed UIColorHex(0xe12228)
// 透明色
#define kColorClear [UIColor clearColor]
// 白色-如导航栏字体颜色
#define kColorWhite UIColorHex(0xffffff)
#define kColorLightWhite UIColorHex(0xf9f9f9)
#define kColorBgWhite UIColorHex(0xfbfbfb)
// 黑色-如输入框输入字体颜色或标题颜色
#define kColorBlack UIColorHex(0x333333)
// 黑色-细黑
#define kColorLightBlack UIColorHex(0x999999)
// 黑色-纯黑
#define kColorDeepBlack UIColorHex(0x000000)
// 灰色—如列表cell分割线颜色样式
#define kColorSeparatorline UIColorHex(0xdddddd)
// 灰色-边框线颜色
#define kColorBorderline UIColorHex(0xb8b8b8)
// 按钮不可用背景色
#define kColorGrayButtonDisable UIColorHex(0xdcdcdc)
// 绿色-如导航栏背景颜色
#define kColorGreenNavBground UIColorHex(0x38ad7a)
// 绿色
#define kColorGreen UIColorHex(0x349c6f)
// 深绿色
#define kColorDarkGreen UIColorHex(0x188d5a)
// 橙色
#define kColorOrange UIColorHex(0xf39700)
// 深橙色
#define kColorDarkOrange UIColorHex(0xe48437)
// 淡紫色
#define kColorLightPurple UIColorHex(0x909af8)
// 红色
#define kColorRed UIColorHex(0xfd492e)
#define kColorLightRed UIColorHex(0xe4393c)
// 蓝色
#define kColorBlue UIColorHex(0x00a0e9)
#define kColorLightBlue UIColorHex(0x3985ff)


// MARK: - block Self
#pragma mark -   weakify( x ) && strongify( x )
/// block self
#ifndef    weakify
#if __has_feature(objc_arc)

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")

#else

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")

#endif
#endif

#ifndef    strongify
#if __has_feature(objc_arc)

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")

#else

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")

#endif


// MARK: - fontSize


// *号字体
#define kFontSize(size) [UIFont systemFontOfSize:size]
/// *号粗体
#define kFontSizeBold(size) [UIFont boldSystemFontOfSize:size]

/// 9号字体
#define kFontSize9 [UIFont systemFontOfSize:9.0]

/// 10号字体
#define kFontSize10 [UIFont systemFontOfSize:10.0]

/// 11号字体
#define kFontSize11 [UIFont systemFontOfSize:11.0]

/// 12号字体
#define kFontSize12 [UIFont systemFontOfSize:12.0]

/// 13号字体
#define kFontSize13 [UIFont systemFontOfSize:13.0]

/// 14号字体
#define kFontSize14 [UIFont systemFontOfSize:14.0]

/// 15号字体
#define kFontSize15 [UIFont systemFontOfSize:15.0]

/// 16号字体
#define kFontSize16 [UIFont systemFontOfSize:16.0]

/// 17号字体
#define kFontSize17 [UIFont systemFontOfSize:17.0]

/// 18号字体
#define kFontSize18 [UIFont systemFontOfSize:18.0]

/// 19号字体
#define kFontSize19 [UIFont systemFontOfSize:19.0]

/// 20号字体
#define kFontSize20 [UIFont systemFontOfSize:20.0]

/// 21号字体
#define kFontSize21 [UIFont systemFontOfSize:21.0]

/// 22号字体
#define kFontSize22 [UIFont systemFontOfSize:22.0]

/// 24号字体
#define kFontSize24 [UIFont systemFontOfSize:24.0]

/// 25号字体
#define kFontSize25 [UIFont systemFontOfSize:25.0]

/// 30号字体
#define kFontSize30 [UIFont systemFontOfSize:30.0]

/// 50号字体
#define kFontSize50 [UIFont systemFontOfSize:50.0]

/// 60号字体
#define kFontSize60 [UIFont systemFontOfSize:60.0]

#pragma mark 大小-粗体

/// 10号粗字体
#define kFontSizeBold10 [UIFont boldSystemFontOfSize:10.0]

/// 11号粗字体
#define kFontSizeBold11 [UIFont boldSystemFontOfSize:11.0]

/// 12号粗字体
#define kFontSizeBold12 [UIFont boldSystemFontOfSize:12.0]

/// 13号粗字体
#define kFontSizeBold13 [UIFont boldSystemFontOfSize:13.0]

/// 14号粗字体
#define kFontSizeBold14 [UIFont boldSystemFontOfSize:14.0]

/// 15号粗字体
#define kFontSizeBold15 [UIFont boldSystemFontOfSize:15.0]

/// 16号粗字体
#define kFontSizeBold16 [UIFont boldSystemFontOfSize:16.0]

/// 17号粗字体
#define kFontSizeBold17 [UIFont boldSystemFontOfSize:17.0]

/// 18号粗字体
#define kFontSizeBold18 [UIFont boldSystemFontOfSize:18.0]

/// 19号粗字体
#define kFontSizeBold19 [UIFont boldSystemFontOfSize:19.0]

/// 20号字粗体
#define kFontSizeBold20 [UIFont boldSystemFontOfSize:20.0]

/// 21号字粗体
#define kFontSizeBold21 [UIFont boldSystemFontOfSize:21.0]

/// 22号字粗体
#define kFontSizeBold22 [UIFont boldSystemFontOfSize:22.0]

/// 23号字粗体
#define kFontSizeBold23 [UIFont boldSystemFontOfSize:23.0]

/// 24号字粗体
#define kFontSizeBold24 [UIFont boldSystemFontOfSize:24.0]

////////////////////////////////////////////////////////////////////////////////////

/*
各版本尺寸
1 iPhone4      640*960   320*480 2倍
2 iPhone4S     640*960   320*480 2倍
3 iPhone5      640*1136  320*568 2倍
4 iPhone5S     640*1136  320*568 2倍
5 iPhone5C     640*1136  320*568 2倍
6 iPhone6      750*1334  375*667 2倍
7 iPhone6 Plus 1242*2208 414*736 3倍

各版本比例
iPhone5,    autoSizeScaleX=1,autoSizeScaleY=1;
iPhone6,    autoSizeScaleX=1.171875,autoSizeScaleY=1.17429577;
iPhone6Plus,autoSizeScaleX=1.29375, autoSizeScaleY=1.295774;
*/

#define IS_IPAD_AutoSize             (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE_AutoSize           (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA_AutoSize           ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH_AutoSize        ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT_AutoSize       ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH_AutoSize   (MAX(SCREEN_WIDTH_AutoSize, SCREEN_HEIGHT_AutoSize))
#define SCREEN_MIN_LENGTH_AutoSize   (MIN(SCREEN_WIDTH_AutoSize, SCREEN_HEIGHT_AutoSize))

#define IS_IPHONE_4_OR_LESS_AutoSize (IS_IPHONE_AutoSize && SCREEN_MAX_LENGTH_AutoSize < 568.0)
#define IS_IPHONE_5_AutoSize         (IS_IPHONE_AutoSize && SCREEN_MAX_LENGTH_AutoSize == 568.0)
#define IS_IPHONE_6_AutoSize         (IS_IPHONE_AutoSize && SCREEN_MAX_LENGTH_AutoSize == 667.0)
#define IS_IPHONE_6P_AutoSize        (IS_IPHONE_AutoSize && SCREEN_MAX_LENGTH_AutoSize == 736.0)

////////////////////////////////////////////////////////////////////////////////////

#define AutoSizeDelegate_AutoSize     ([[UIApplication sharedApplication] delegate])
#define AutoSizeScreenWidth_AutoSize  ([[UIScreen mainScreen] bounds].size.width)
#define AutoSizeScreenHeight_AutoSize ([[UIScreen mainScreen] bounds].size.height)

#define AutoSizeScaleX_AutoSize ((AutoSizeScreenHeight_AutoSize > 480.0) ? (AutoSizeScreenWidth_AutoSize / 320.0) : 1.0)
#define AutoSizeScaleY_AutoSize ((AutoSizeScreenHeight_AutoSize > 480.0) ? (AutoSizeScreenHeight_AutoSize / 568.0) : 1.0)

////////////////////////////////////////////////////////////////////////////////////

CG_INLINE CGFloat
AutoCGRectGetMinX(CGRect rect)
{
   CGFloat x = rect.origin.x * AutoSizeScaleX_AutoSize;
   return x;
}

CG_INLINE CGFloat
AutoCGRectGetMinY(CGRect rect)
{
   CGFloat y = rect.origin.y * AutoSizeScaleX_AutoSize;
   return y;
}

CG_INLINE CGFloat
AutoCGRectGetWidth(CGRect rect)
{
   CGFloat width = rect.size.width * AutoSizeScaleX_AutoSize;
   return width;
}

CG_INLINE CGFloat
AutoCGRectGetHeight(CGRect rect)
{
   CGFloat height = rect.size.height * AutoSizeScaleX_AutoSize;
   return height;
}

CG_INLINE CGPoint
AutoCGPointMake(CGFloat x, CGFloat y)
{
   CGPoint point;
   point.x = x * AutoSizeScaleX_AutoSize;
   point.y = y * AutoSizeScaleY_AutoSize;
   
   return point;
}

CG_INLINE CGSize
AutoCGSizeMake(CGFloat width, CGFloat height)
{
   CGSize size;
   size.width = width * AutoSizeScaleX_AutoSize;
   size.height = height * AutoSizeScaleY_AutoSize;
   
   return size;
}

CG_INLINE CGRect
AutoCGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
   CGRect rect;
   rect.origin.x = x * AutoSizeScaleX_AutoSize;
   rect.origin.y = y * AutoSizeScaleY_AutoSize;
   rect.size.width = width * AutoSizeScaleX_AutoSize;
   rect.size.height = height * AutoSizeScaleY_AutoSize;
   
   return rect;
}

////////////////////////////////////////////////////////////////////////////////////

CG_INLINE CGRect
AutoWHCGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height, BOOL autoW, BOOL autoH)
{
   CGRect rect;
   rect.origin.x = x;
   rect.origin.y = y;
   rect.size.width = (autoW ? (width * AutoSizeScaleX_AutoSize) : width);
   rect.size.height = (autoH ? (height * AutoSizeScaleY_AutoSize) : height);
   
   return rect;
}

CG_INLINE CGFloat
AutoWHGetHeight(CGFloat height)
{
   CGFloat autoHeight = height * AutoSizeScaleY_AutoSize;
   return autoHeight;
}

CG_INLINE CGFloat
AutoWHGetWidth(CGFloat width)
{
   CGFloat autoWidth = width * AutoSizeScaleX_AutoSize;
   return autoWidth;
}

#endif
#endif /* PrefixHeader_pch */

你可能感兴趣的:(iOS常用的宏定义(实用tips))