记录下iOS开发中的常见的宏。

常用宏

//屏幕宽高
#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height 

//颜色
#define YJColor(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]
#define YJRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
#define HEXCOLOR(c)  [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0];
 
 
#define NSStringFromBool(b) (b ? @"YES" : @"NO")

 
//weakSelf
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self 

//StringValue
#define StringValue(s) ([s isKindOfClass:[NSNull class]] || [s isEqual:[NSNull null]] || s == nil)?@"":s

#define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]])) 

#define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)

//角度转弧度 弧度转角度
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180) 
#define Radian_To_Degrees(r) (r*180.0)/(M_PI)

沙盒

#define kPathTemp                   NSTemporaryDirectory()
#define kPathDocument               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathCache                  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]

System Version

//选自YYText
static double _YYDeviceSystemVersion() {
    static double version;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        version = [UIDevice currentDevice].systemVersion.doubleValue;
    });
    return version;
}

#ifndef kSystemVersion
#define kSystemVersion _YYDeviceSystemVersion()
#endif

#ifndef kiOS6Later
#define kiOS6Later (kSystemVersion >= 6)
#endif

#ifndef kiOS7Later
#define kiOS7Later (kSystemVersion >= 7)
#endif

#ifndef kiOS8Later
#define kiOS8Later (kSystemVersion >= 8)
#endif

#ifndef kiOS9Later
#define kiOS9Later (kSystemVersion >= 9)
#endif

DLog

#ifdef DEBUG
#define LRString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define DLog(...) printf("%s: [%s:%d]: %s\n\n",[[NSString yj_stringDate] UTF8String], [LRString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
#define DLog(...)
#endif 

// yj_stringDate 是NSString的类别方法
+ (NSString *)yj_stringDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

系统宏:

// ARC 
#if __has_feature(objc_arc) 
    //arc
#else 
    //mrc 
#endif
#ifdef __OBJC__
    // Objectice-C 
#endif

#if TARGET_OS_IPHONE 
    // iPhone 
#endif 

#if TARGET_IPHONE_SIMULATOR 
    //Simulator
#endif  

//描述方法iOS10引入
- (void)myMethod NS_AVAILABLE_IOS(10_0);

//描述方法iOS2引入iOS6废弃
- (void)myMethod2  NS_DEPRECATED_IOS(2_0, 6_0) ;

//描述方法被禁用
//http://stackoverflow.com/questions/4924285/how-to-deprecate-a-method-in-xcode
//It defined in usr/include/AvailabilityMacros.h:
//#define DEPRECATED_ATTRIBUTE        __attribute__((deprecated))
//#define DEPRECATED_MSG_ATTRIBUTE(msg) __attribute((deprecated((msg))))
-(void)foo:(id)x  DEPRECATED_ATTRIBUTE;
- (void)bar:(id)x DEPRECATED_MSG_ATTRIBUTE("Use baz: method instead.")

补充

设置view圆角和边框

#define LRViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]