iOS -- 常用pch宏

//屏幕寬、高
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//1個像素的寬度
#define SINGLE_LINE_WIDTH (1.0f/[UIScreen mainScreen].scale)
//系統版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//圖片
#define ImageWithName(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
//rgb顏色(十進制)
#define UIColorFromRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
//rgb顏色(十六進制)
#define UIColorFromHexRGB(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] \

//DEBUG模式下,打印日誌(包括函數名、行號)
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif

像這些宏定義,在工程中全局都需要經常使用,我們可以把它放入工程中的PrefixHeader.pch文件中.具體步驟如下:

  1. 新建PrefixHeader.pch文件,如圖:

  2. PrefixHeader.pch中添加我們需要使用的宏定義,如圖:

  3. 在 Prefix Header 設置路徑, 具體位置在 TARGET ->Build Settings -> Prefix Header 中,如圖:

這樣我們的 pch 文件就添加完成了, 工程中全局就可以使用這些宏定義了.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章