iOS_开发中常用宏定义

///MARK:  Color 
#define RGBA(_r, _g, _b,_a) [UIColor colorWithRed:(_r)/255.0 green:(_g)/255.0 blue:(_b)/255.0 alpha:(_a)]
#define RGB(_r, _g, _b)     [UIColor colorWithRed:(_r)/255.0 green:(_g)/255.0 blue:(_b)/255.0 alpha:1]
#define HexColor(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 Random_Color        [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
///MARK:  Weak 
#define WeakObject(object)      __weak typeof(object) weak##object = object
#define StrongObject(object)    __typeof(weak##object) object = weak##object
///MARK:  Image 
#define Image(imageName)     [UIImage imageNamed:imageName]
#define Image_BundleFile(filename,filetype)     [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]

///MARK:  Frame 

  #define kScreenWidth        [UIScreen mainScreen].bounds.size.width
  #define kScreenHeight       [UIScreen mainScreen].bounds.size.height
  #define kScreenScale        [UIScreen mainScreen].scale
  #define kScreenBounds       [UIScreen mainScreen].bounds
  #define kTabBarHeight       [[UITabBarController alloc] init].tabBar.frame.size.height
    ///MARK:  UserDefaults 
  //存储对象
  #define UserDefaultSetObjectForKey(__VALUE__,__KEY__) \
   {\
  [[NSUserDefaults standardUserDefaults] setObject:__VALUE__       forKey:__KEY__];\
  [[NSUserDefaults standardUserDefaults] synchronize];\
  }
  //获得存储的对象
  #define UserDefaultObjectForKey(__KEY__)  [[NSUserDefaults       standardUserDefaults] objectForKey:__KEY__]
  //删除对象
  #define UserDefaultRemoveObjectForKey(__KEY__) \
  {\
  [[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
  [[NSUserDefaults standardUserDefaults] synchronize];\
  }
  
  //字符串是否为空
    #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )

    #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]

    //判断是真机还是模拟器
    #if TARGET_OS_IPHONE
    //真机
    #endif

    #if TARGET_IPHONE_SIMULATOR
    //模拟器
    #endif

    //开发的时候打印,但是发布的时候不打印的NSLog
    #ifdef DEBUG
    #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    #else
    #define NSLog(...)
    #endif

    //颜色
    #define kRGBColor(r, g, b)     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
    #define kRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
    #define kRandomColor  KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)

    #define kColorWithHex(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 kWeakSelf(type)   __weak typeof(type) weak##type = type;
    #define kStrongSelf(type) __strong typeof(type) type = weak##type;

    //由角度转换弧度
    #define kDegreesToRadian(x)      (M_PI * (x) / 180.0)
    //由弧度转换角度
    #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)

    //获取一段时间间隔
    #define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    #define kEndTime   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

    //重置frame 
    #define RESET_FRAME_ORIGIN_X(view, x)                      view.frame = (CGRect){x, ORIGIN_Y(view), SIZE_W(view), SIZE_H(view)}
    #define RESET_FRAME_ORIGIN_Y(view, y)                    view.frame = (CGRect){ORIGIN_X(view), y, SIZE_W(view), SIZE_H(view)}
    #define RESET_FRAME_SIZE_WIDTH(view, width)              view.frame = (CGRect){ORIGIN_X(view), ORIGIN_Y(view), width, SIZE_H(view)}
      #define RESET_FRAME__SIZE_HEIGHT(view, height)           view.frame = (CGRect){ORIGIN_X(view), ORIGIN_Y(view), SIZE_W(view), height}

    #define ORIGIN(view)                                     (view.frame.origin)
    #define SIZE(view)                                       (view.frame.size)
    #define FRAME(view)                                      (view.frame)
    #define ORIGIN_X(view)                                   (view.frame.origin.x)
    #define ORIGIN_Y(view)                                   (view.frame.origin.y)
    #define SIZE_W(view)                                     (view.frame.size.width)
    #define SIZE_H(view)                                     (view.frame.size.height)
    #define ORIGIN_Y_ADD_SIZE_H(view)                        (ORIGIN_Y(view) + SIZE_H(view))
    #define ORIGIN_X_ADD_SIZE_W(view)                        (ORIGIN_X(view) + SIZE_W(view))

你可能感兴趣的:(iOS_开发中常用宏定义)