iOS | 小笔记:常用宏定义

  • 手机系统
#define IS_PAD   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IOS7  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
#define IS_IOS8  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
#define IS_IOS9  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9)
#define IS_IOS10 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10)
#define IS_IOS11 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11)
  • 手机系统版本,如9.3
#define kSystemVersion [[UIDevice currentDevice] systemVersion]
  • 系统名称,如iOS
#define kSystemName [[UIDevice currentDevice] systenName]
  • UUID
#define UUID [UIDevice currentDevice].identifierForVendor.UUIDString
  • APP版本号
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
  • 色值
#define RGB(r, g, b)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RGBA(r, g, b, a)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define 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]
#define HEXA(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]
  • 字体大小(常规/粗体)
#define FONT(NAME, FONTSIZE)     [UIFont fontWithName:(NAME) size:(FONTSIZE)]
#define SYSTEMFONT(FONTSIZE)     [UIFont systemFontOfSize:FONTSIZE]
#define BOLDSYSTEMFONT(FONTSIZE) [UIFont boldSystemFontOfSize:FONTSIZE]
  • 不同屏幕适配
#define kScreenWidthRatio        (Main_Screen_Width / 375.0)
#define kScreenHeightRatio       (Main_Screen_Height / 667.0)
#define AdaptedFont(font)        SYSTEMFONT(AdaptedWidth(font))
#define AdaptedWidth(width)      ceilf((width) * kScreenWidthRatio)
#define AdaptedHeight(height)    ceilf((height) * kScreenHeightRatio)
  • 屏幕宽高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
  • 状态栏高度
#define kStatusBarHeight [UIApplicationsharedApplication].statusBarFrame.size.height
  • 导航栏高度(包含状态栏)
#define kNavBar_Height (kStatusBar_Height + 44)
  • TabBar高度
#define kTabBarHeight (CGFloat)([[CommonUtility phoneModel] isEqualToString:@"iPhone X"]?(49+34):(49))
  • Application
#define kApplication [UIApplication sharedApplication]
  • AppDelegate
#define kAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
  • Window
#define kKeyWindow   [UIApplication sharedApplication].keyWindow
  • Temp 文件路径
#define kPathTemp NSTemporaryDirectory()
  • Document 文件路径
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  • Cache 文件路径
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  • 弱引用强引用
//可配对引用在外面用weakSelf(self),block用strongSelf(self)
//也可以单独引用在外面用weakSelf(self) block里面用weakself
#define weakSelf(type)  __weak typeof(type) weak##type = type;
#define strongSelf(type)  __strong typeof(type) type = weak##type;
  • 由角度转换弧度
#define kAngleToRadian(x)      (M_PI * (x) / 180.0)
  • 由弧度转换角度
#define kRadianToAngle(radian) (radian * 180.0) / (M_PI)

你可能感兴趣的:(iOS | 小笔记:常用宏定义)