iOS - 常用宏定义和PCH文件知识点整理

发布时间:2019-05-08 22:00:18发布者:Mr.Zhang阅读(193)

(一)PCH文件操作步骤演示:

第一步:图文所示:

第二步:图文所示:

 

(二)常用宏定义整理:

(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)常用屏幕适配宏(方法有很多可以灵活运用)

//============================================================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)其他宏运用补充:

//============================================================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;

 

(三)后续常用宏定义会慢慢补充;





本文转自博客园,原文地址:https://www.cnblogs.com/dingding3w/p/10831386.html