iOS - 經常使用宏定義和PCH文件知識點整理

(一)PCH文件操做步驟演示:安全

第一步:圖文所示:iphone

第二步:圖文所示:函數

 

(二)經常使用宏定義整理:spa

(1)經常使用Log日誌宏(輸出日誌詳細可定位某個類、某個函數、某一行)日誌

//============================================================NSLog
#ifdef DEBUG
#define DDLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define BASE_URL @""
#else
#define DDLog(...)
#define BASE_URL @""
#endif

#ifdef DEBUG
#define DDString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define DDLog(...) printf("%s: %s [%d]: %s\n\n",[[NSString dh_stringDate] UTF8String], [DDString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#define BASE_URL @""
#else
#define DDLog(...)
#define BASE_URL @""
#endif
//============================================================(第二個比較詳細適應於真機模擬器)

 

#pragma mark - 其中第二個宏定義中會用到(NSString分類文件,該文件以下)
// NSString+DHExtension.h 文件中
#import <Foundation/Foundation.h>

@interface NSString (DHExtension)
/**
 Log日誌會用到
 
 @return NSString
 */
+ (NSString *)dh_stringDate;
@end


// NSString+DHExtension.m 文件中
#import "NSString+DHExtension.h"

@implementation NSString (DHExtension)
#pragma mark - Log日誌會用到
+ (NSString *)dh_stringDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}
@end

 

(2)經常使用屏幕適配宏(方法有不少能夠靈活運用)code

//============================================================UIScreen/iPhone機型判斷
// 屏幕寬
#define DDScreenW [UIScreen mainScreen].bounds.size.width
// 屏幕高
#define DDScreenH [UIScreen mainScreen].bounds.size.height

// 用戶機型判斷宏
#define DDCurrentModeSize [[UIScreen mainScreen] currentMode].size
// 判斷是不是iPad
#define DDiPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
// 判斷iPhone4系列
#define DDiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPhone5系列
#define DDiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPhone6系列(包含iPhone6/iPhone7/iPhone8)
#define DDiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iphone6P系列(包含iPhone6P/iPhone7P/iPhone8P)
#define DDiPhoneP ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPhoneX
#define DDiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPHoneXr
#define DDiPhoneXr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPhoneXs
#define DDiPhoneXs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), DDCurrentModeSize) && !DDiPad : NO)
// 判斷iPhoneXs Max
#define DDiPhoneXsMax ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), DDCurrentModeSize) && !DDiPad : NO)

// 頂部NavBar高度
#define DDNavBarH ((DDiPhoneX==YES || DDiPhoneXr==YES || DDiPhoneXs==YES || DDiPhoneXsMax==YES) ? 88.0 : 64.0)
// 底部TabBar高度
#define DDTabBarH ((DDiPhoneX==YES || DDiPhoneXr==YES || DDiPhoneXs==YES || DDiPhoneXsMax==YES) ? 83.0 : 49.0)
// 頂部StatusBar高度
#define DDStatusBarH ((DDiPhoneX==YES || DDiPhoneXr==YES || DDiPhoneXs==YES || DDiPhoneXsMax==YES) ? 44.0 : 20.0)
// 判斷是否是劉海屏(iPhoneX系列)
#define DDiPhoneNewX ((DDNavBarH==88.0 || DDiPhoneXr==83.0 || DDStatusBarH==44.0) ? YES : NO)
// 底部安全區域高度
#define DDTabbarSafeH (DDiPhoneNew ? 34.f : 0.f)
// 頂部適配系統方法
#define STATUS_BAR_FRAME [[UIApplication sharedApplication] statusBarFrame]
// iPhoneX系列判斷(包含iPhone X/iPhone XS/iPhone XS Max/iPhone XR)
#define  DDIS_iPhoneX (CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size)  || CGSizeEqualToSize(CGSizeMake(414.f, 896.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(896.f, 414.f), [UIScreen mainScreen].bounds.size))
//============================================================UIScreen/iPhone機型判斷(方法有不少種靈活運用)

(3)其餘宏運用補充:orm

//============================================================RGB
#define DDRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define DDBackGroundRGB DDRGBColor(236, 236, 236)
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0green:(((s &0xFF00) >>8))/255.0blue:((s &0xFF))/255.0alpha:1.0]

//============================================================默認圖片
#define DDPlaceImage [UIImage imageNamed:@"imageName.png"]

//============================================================SELF
#define WeakSelf(weakSelf)  __weak __typeof(self) weakSelf = self;

 

(三)後續經常使用宏定義會慢慢補充;blog

相關文章
相關標籤/搜索