iOS 一些常用的宏定義

規(guī)范

#ifdef __OBJC__
/** Objectice-C */ 
#endif

#if TARGET_OS_IPHONE 
/** iPhone Device */ 
#endif 

#if TARGET_IPHONE_SIMULATOR 
/** iPhone Simulator */ 
#endif 

// ARC 
#if __has_feature(objc_arc) 
/** Compiling with ARC */ 
#else 
/** Compiling without ARC */ 
#endif

宏定義

// 測試方法
#ifdef DEBUG
  #define LSLog(...) NSLog(__VA_ARGS__)
#else
  #define LSLog(...)
#endif

// 測試方法
#ifdef DEBUG 
#define DLog( s, ... ) NSLog( @"< %@:(%d) > %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) 
#else 
#define DLog( s, ... ) 
#endif

//隨機色
#define LSRandomColor [UIColor colorWithRed:((float)arc4random_uniform(256) /255.0) green:((float)arc4random_uniform(256) /255.0) blue:((float)arc4random_uniform(256) /255.0) alpha:1.0];

// 顏色(RGB) 
#define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1] 
#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

// RGB顏色轉換(16進制->10進制) 
#define UIColorFromRGB(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]

//首次啟動判斷:
#define First_Launched @"firstLaunch"

// Screen Scale 
#define MainScreenScale [[UIScreen mainScreen] scale] 

// App Frame 
#define Application_Frame [[UIScreen mainScreen] applicationFrame] 

// App Frame Height&Width 
#define App_Frame_Height [[UIScreen mainScreen] applicationFrame].size.height
 #define App_Frame_Width [[UIScreen mainScreen] applicationFrame].size.width

// MainScreen Height&Width 
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height 
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width 

// View 坐標(x,y)和寬高(width,height) 
#define X(v) (v).frame.origin.x 
#define Y(v) (v).frame.origin.y 

#define WIDTH(v) (v).frame.size.width
#define HEIGHT(v) (v).frame.size.height 

#define MinX(v) CGRectGetMinX((v).frame) 
#define MinY(v) CGRectGetMinY((v).frame) 
#define MidX(v) CGRectGetMidX((v).frame) 
#define MidY(v) CGRectGetMidY((v).frame) 
#define MaxX(v) CGRectGetMaxX((v).frame) 
#define MaxY(v) CGRectGetMaxY((v).frame)

// 系統(tǒng)控件默認高度 
#define kStatusBarHeight (20.f) //電源所在的狀態(tài)欄
#define kTopBarHeight (44.f) //NavigationBar的高度
#define kBottomBarHeight (49.f) //Tabbar的高度
#define kCellDefaultHeight (44.f) //UITableViewCell 的默認高度
#define kEnglishKeyboardHeight (216.f)//英語鍵盤的默認高度
#define kChineseKeyboardHeight (252.f)//漢語鍵盤的默認高度

// PNG JPG 圖片路徑 
#define PNGPATH(NAME) [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:NAME] ofType:@"png"] 
#define JPGPATH(NAME) [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:NAME] ofType:@"jpg"] 
#define PATH(NAME, EXT) [[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)] 

// 加載圖片 
#define PNGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"png"]] 
#define JPGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"jpg"]] 
#define IMAGE(NAME, EXT) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)]] 
#define IMAGENAMED(NAME) [UIImage imageNamed:NAME]

// 字體大小(常規(guī)/粗體) 
#define BOLDSYSTEMFONT(FONTSIZE)[UIFont boldSystemFontOfSize:FONTSIZE] 
#define SYSTEMFONT(FONTSIZE) [UIFont systemFontOfSize:FONTSIZE] 
#define FONT(NAME, FONTSIZE) [UIFont fontWithName:(NAME) size:(FONTSIZE)]

// 當前版本 
#define FSystemVersion ([[[UIDevice currentDevice] systemVersion] floatValue]) 
#define DSystemVersion ([[[UIDevice currentDevice] systemVersion] doubleValue]) 
#define SSystemVersion ([[UIDevice currentDevice] systemVersion])
//ios7系統(tǒng)判斷:
#define IsIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO)

// 當前語言 
#define CURRENTLANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0]) 

// 是否Retina屏 
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? \ CGSizeEqualToSize(CGSizeMake(640, 960), \ [[UIScreen mainScreen] currentMode].size) : \ NO)

// 是否iPhone5 
#define isiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? \ CGSizeEqualToSize(CGSizeMake(640, 1136), \ [[UIScreen mainScreen] currentMode].size) : \ NO)


//另一種方式判斷版本
#define IS_OS_5_OR_LATER     ([[[UIDevice currentDevice] systemVersion] compare:@"5.0" options:NSNumericSearch] != NSOrderedAscending) //iPhone5或者更高版本
#define IS_OS_6_OR_LATER     ([[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending) //iPhone6或者更高版本
#define IS_OS_7_OR_LATER     ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) //iPhone7或者更高版本

// 是否 iPad 
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

// 是否是 iPhone
#define isPhone ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)

// 本地化字符串
/** NSLocalizedString宏做的其實就是在當前bundle中查找資源文件名“Localizable.strings”(參數(shù):鍵+注釋) */ 
#define LocalString(x, ...) NSLocalizedString(x, nil) 
/** NSLocalizedStringFromTable宏做的其實就是在當前bundle中查找資源文件名“xxx.strings”(參數(shù):鍵+文件名+注釋) */ 
#define AppLocalString(x, ...) NSLocalizedStringFromTable(x, @"zjj", nil) 

// App Name 
#define AppDisplayName [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]

/** 時間間隔 */ 
#define kHUDDuration (1.f) 
/** 一天的秒數(shù) */ 
#define SecondsOfDay (24.f * 60.f * 60.f) 
/** 秒數(shù) */ 
#define Seconds(Days) (24.f * 60.f * 60.f * (Days)) 
/** 一天的毫秒數(shù) */ 
#define MillisecondsOfDay (24.f * 60.f * 60.f * 1000.f) 
/** 毫秒數(shù) */ 
#define Milliseconds(Days) (24.f * 60.f * 60.f * 1000.f * (Days))

//判斷
#define IFISNIL(v)                      (v = (v != nil) ? v : @"")
#define IFISNILFORNUMBER(v)             (v = (v != nil) ? v : [NSNumber numberWithInt:0])
#define IFISSTR(v)                      (v = ([v isKindOfClass:[NSString class]]) ? v : [NSString stringWithFormat:@"%@",v])

系統(tǒng)中自帶的宏

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif#ifdef __OBJC__    
#import <UIKit/UIKit.h>    
#import <Foundation/Foundation.h>
#endif

系統(tǒng)中宏使用零碎

CGFloat min=MIN(A,B);//最終的大小取決于B,但是最大不能超過A炭分,如果超過A那么值就等于A赴肚。
CGFloat max=MAX(A,B);
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末曲秉,一起剝皮案震驚了整個濱河市炼团,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌击吱,老刑警劉巖淋淀,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異覆醇,居然都是意外死亡朵纷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門永脓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來袍辞,“玉大人,你說我怎么就攤上這事常摧〗劣酰” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長似芝。 經(jīng)常有香客問我那婉,道長板甘,這世上最難降的妖魔是什么党瓮? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮盐类,結果婚禮上寞奸,老公的妹妹穿的比我還像新娘。我一直安慰自己在跳,他們只是感情好枪萄,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著猫妙,像睡著了一般瓷翻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上割坠,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天齐帚,我揣著相機與錄音,去河邊找鬼彼哼。 笑死对妄,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的敢朱。 我是一名探鬼主播剪菱,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拴签!你這毒婦竟也來了孝常?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蚓哩,失蹤者是張志新(化名)和其女友劉穎茫因,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杖剪,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡冻押,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了盛嘿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片洛巢。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖次兆,靈堂內(nèi)的尸體忽然破棺而出稿茉,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布漓库,位于F島的核電站恃慧,受9級特大地震影響,放射性物質發(fā)生泄漏渺蒿。R本人自食惡果不足惜痢士,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望茂装。 院中可真熱鬧怠蹂,春花似錦、人聲如沸少态。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽彼妻。三九已至嫌佑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間侨歉,已是汗流浹背屋摇。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留为肮,地道東北人摊册。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像颊艳,于是被迫代替她去往敵國和親茅特。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容