iOS開發常用宏定義

宏定義可以在我們平時的開發中幫助我們更簡潔有效的進行編程,以下是我工作中遇到和蒐集整理的一些宏定義:

關於UI設置

/*UI顏色的快捷設置/

#define kHEXCOLOR(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 kColorFromRGBA(r, g, b , a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

//隨即顏色
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

//設置RGB顏色/設置RGBA顏色
#define kRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

// clear背景顏色
#define LRClearColor [UIColor clearColor]

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

#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]   //返回一個RGBA格式的UIColor對象
#define RGB(r, g, b) RGBA(r, g, b, 1.0f) // 返回一個RGB格式的UIColor對象

//通用顏色
#define kBlackColor         [UIColor blackColor]
#define kDarkGrayColor      [UIColor darkGrayColor]
#define kLightGrayColor     [UIColor lightGrayColor]
#define kWhiteColor         [UIColor whiteColor]
#define kGrayColor          [UIColor grayColor]
#define kRedColor           [UIColor redColor]
#define kGreenColor         [UIColor greenColor]
#define kBlueColor          [UIColor blueColor]
#define kCyanColor          [UIColor cyanColor]
#define kYellowColor        [UIColor yellowColor]
#define kMagentaColor       [UIColor magentaColor]
#define kOrangeColor        [UIColor orangeColor]
#define kPurpleColor        [UIColor purpleColor]
#define kClearColor         [UIColor clearColor]

/**
設置 view 圓角和邊框
*/

#define JLViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
字符串是否爲空

  #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
數組是否爲空

  #define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
字典是否爲空

  #define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
APP版本號

  #define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
系統版本號

  #define kSystemVersion [[UIDevice currentDevice] systemVersion]
獲取當前語言
  #define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
判斷是否爲iPhone

  #define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  
  判斷是否爲iPad

  #define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
獲取沙盒Document路徑

  #define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
獲取沙盒temp路徑

  #define kTempPath NSTemporaryDirectory()
獲取沙盒Cache路徑

  #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
//開發的時候打印,但是發佈的時候不打印的NSLog

  #ifdef DEBUG
  #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
  #else
   #define NSLog(...)
   #endif

//pragma mark -- 引用

#define STWeakSelf __weak typeof(self) weakSelf = self; // 弱引用
#define LRWeakSelf(type)  __weak typeof(type) weak##type = type; //弱引用
#define LRStrongSelf(type)  __strong typeof(type) strong##type = weak##type;   //強引用

//pragma mark -- 方法

#ifdef DEBUG
#define MyLog(...)  NSLog(__VA_ARGS__)
#else
#define MyLog(...)
#endif

//判斷字符串是否爲空

+ (BOOL)isNullOrEmpty:(NSString *)string
{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
+ (NSString *) convertStringValue:(NSString *)string
{
    if (string == nil || string == NULL) {
        return @"";
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return @"";
    }
    return string;
}

宏定義一個單例模式

單例化一個類

  #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

 

//獲取當前版本號
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  
   CFShow(infoDictionary);  
  // app名稱  
   NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];  
   // app版本  
   NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  
   // app build版本  
   NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];  

 

    //手機序列號  

   NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];  
    NSLog(@"手機序列號: %@",identifierNumber); 
    
    //手機別名:
    用戶定義的名稱  
    NSString* userPhoneName = [[UIDevice currentDevice] name];  
    NSLog(@"手機別名: %@", userPhoneName);  
    //設備名稱  
    NSString* deviceName = [[UIDevice currentDevice] systemName];  
    NSLog(@"設備名稱: %@",deviceName );  
    //手機系統版本  
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];  
    NSLog(@"手機系統版本: %@", phoneVersion);  
    //手機型號  
    NSString* phoneModel = [[UIDevice currentDevice] model];  
    NSLog(@"手機型號: %@",phoneModel );  
    //地方型號  (國際化區域名稱)  
    NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];  
    NSLog(@"國際化區域名稱: %@",localPhoneModel );  
      
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  
    // 當前應用名稱  
    NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];  
    NSLog(@"當前應用名稱:%@",appCurName);  
    // 當前應用軟件版本  比如:1.0.1  
    NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  
    NSLog(@"當前應用軟件版本:%@",appCurVersion);  
    // 當前應用版本號碼   int類型  
    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];  
    NSLog(@"當前應用版本號碼:%@",appCurVersionNum); 

 

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