iOS常用的宏定義

1.尺寸,屏幕的寬高

#define JK_WIDTH  [UIScreen mainScreen].bounds.size.width
#define JK_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define JKSizeScale ((CIO_SCREEN_HEIGHT > 667) ? CIO_SCREEN_HEIGHT/667 : 1)

2.顏色

透明色
#define kClearColor [UIColor clearColor]
顏色的RGB
#define JKRGBColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
隨機(jī)色
#define JKRandomColor  [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0]
帶透明度的RGB
#define JKRGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
rgb顏色轉(zhuǎn)換(16進(jìn)制->10進(jìn)制)
#define JKColorFromRGB(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]

3.字體的大小 font

#define  Font_text_12 [UIFont systemFontOfSize:12]
#define  Font_text_14 [UIFont systemFontOfSize:14]
#define  Font_text_16 [UIFont systemFontOfSize:16]

4. 開發(fā)輸出模式的設(shè)置

#ifdef DEBUG   //處于開發(fā)階段
#define JKNSLog(...) NSLog(__VA_ARGS__)
#else          //處于發(fā)布狀態(tài)(沒有輸出)
#define  JKNSLog(...)
#endif

5.NSUserDefaults 的存取值

//獲得存儲(chǔ)的對象
#define UserDefaultObject(A) [[NSUserDefaults standardUserDefaults]objectForKey:A]
//存值(可變的值不可以存)
#define UserDefaultSetValue(B,C) [[NSUserDefaults standardUserDefaults]setObject:B forKey:C]
//存BOOL值
#define UserDefaultBool(D,E)  [[NSUserDefaults standardUserDefaults]setBool:D forKey:E]
#define  Synchronize          [[NSUserDefaults standardUserDefaults]synchronize]
清除存儲(chǔ)對象
#define UserDefaultRemoveObjectForKey(__KEY__) \
{\
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
    [[NSUserDefaults standardUserDefaults] synchronize];\
}

6. 獲取系統(tǒng)版本

#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])

7.獲取當(dāng)前的語言

#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])

8.通過路徑加載本地的圖片

#define JKLoadImage(filename,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]

9.在沙盒創(chuàng)建文件

 #define JKPLIST_TICKET_INFO_EDIT [NSHomeDirectory() stringByAppendingString:@"/Documents/data.plist"] //edit the plist

10.GCD

#define GCDWithGlobal(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define GCDWithMain(block) dispatch_async(dispatch_get_main_queue(),block)

11.快速查詢一段代碼的執(zhí)行時(shí)間

/*     用法
       TICK
       do your work here
       TOCK
 */

#define TICK NSDate *startTime = [NSDate date];
#define TOCK NSLog(@"Time:%f", -[startTime timeIntervalSinceNow]);

12.機(jī)型的判斷

/* 判斷是否為iPhone */
   #define isiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

/* 判斷是否是iPad */
   #define isiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

/* 判斷是否為iPod */
   #define isiPod ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

13.由角度轉(zhuǎn)換弧度 由弧度轉(zhuǎn)換角度

 degree角度
 radian  弧度
 角度轉(zhuǎn)弧度
 #define LRDegreesToRadian(angle)  (M_PI * (angle) / 180.0)
 弧度轉(zhuǎn)角度
 #define LRRadianToDegrees(radian) (radian*180.0)/(M_PI)

14.弱引用

 #define JKWeakSelf __weak typeof(self) weakSelf = self;

15.打印函數(shù),可以打印所在的函數(shù),行數(shù),以及你要打印的值

#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
NSLog(@"==1==%s 2==[Line %d] 3==%@", __PRETTY_FUNCTION__, __LINE__,string);

16.設(shè)置 view 圓角和邊框

#define LRViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

17.宏定義一個(gè)彈窗方法,括號(hào)里面是方法的參數(shù)

#define JKShowAlert(message)    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: @"OK"];[alert show];

18.為空的判斷

字符串是否為空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
數(shù)組是否為空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
字典是否為空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
是否是空對象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

19.APP版本號(hào)

#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

20.系統(tǒng)版本號(hào)

#define kSystemVersion [[UIDevice currentDevice] systemVersion]

21.獲取t路徑

獲取沙盒Document路徑
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
獲取沙盒temp路徑
#define kTempPath NSTemporaryDirectory()
獲取沙盒Cache路徑
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

22.獲取一段時(shí)間間隔

#define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();

#define kEndTime  NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

23.方正黑體簡體字體定義

 #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]

24.程序的本地化,引用國際化的文件

#define MyLocal(x, ...) NSLocalizedString(x, nil)

25.單例化一個(gè)類

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
  static classname *shared##classname = nil; \
\
 + (classname *)shared##classname \
 { \
   @synchronized(self) \
   { \

   if (shared##classname == nil) \
   { \

    shared##classname = [self alloc] init]; \
   } \

  } \
 \
  return shared##classname; \
   } \
 \
  + (id)allocWithZone:(NSZone *)zone \
  { \
    @synchronized(self) \
     { \

  if (shared##classname == nil) \
  { \

   shared##classname = [super allocWithZone:zone]; \
   return shared##classname; \
   } \
   } \
 \
  return nil; \
 } \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
     return self; \
}
#endif

26.判斷是真機(jī)還是模擬器

\#if TARGET_OS_IPHONE
//真機(jī)  
\#endif 
\#if TARGET_IPHONE_SIMULATOR
//模擬器
\#endif

27.在OC文件導(dǎo)入某些頭文件

#ifdef __OBJC__
 //導(dǎo)入頭文件
#endif

28.首次啟動(dòng)判斷

#define First_Launched @"firstLaunch"

29.GCD 的宏定義

//GCD - 一次性執(zhí)行

#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

//GCD - 在Main線程上運(yùn)行

#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

//GCD - 開啟異步線程

#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

推薦幾篇博客

iOS中關(guān)于宏定義與常量的使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容