iOS 宏定义
sunWaterMood opened this issue · comments
SunWater commented
1 weakself和strongself
ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
endif
ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
endif
2 获取设备大小
define NavigationBar_HEIGHT 44
3 获取屏幕 宽度、高度
define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
4 打印日志
ifdef DEBUG
define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), PRETTY_FUNCTION, LINE, ##VA_ARGS);
else
define DLog(...)
endif
5 重写NSLog,Debug模式下打印日志和当前行数
if DEBUG
define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", FUNCTION, LINE, [NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);
else
define NSLog(FORMAT, ...) nil
endif
6 是否是ipad
define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
7 获取系统版本
define IOS_VERSION [[UIDevice currentDevice] systemVersion] floatValue]
define CurrentSystemVersion [UIDevice currentDevice] systemVersion]
8 获取当前系统的语言
define CurrentLanguage ([NSLocale preferredLanguages] objectAtIndex:0])
9 判断设备的操做系统是不是ios7
define IOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]
10 判断是真机还是模拟器
if TARGET_OS_IPHONE
//iPhone Device
endif
if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
endif
11 颜色值转换
// 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]
//带有RGBA的颜色设置
define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
// 获取RGB颜色
define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
define RGB(r,g,b) RGBA(r,g,b,1.0f)
//背景色
define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
//清除背景色
define CLEARCOLOR [UIColor clearColor]
12 由角度获取弧度 有弧度获取角度
define degreesToRadian(x) (M_PI * (x) / 180.0)
define radianToDegrees(radian) (radian*180.0)/(M_PI)
13 单例
//单例化一个类
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;
}
14 沙盒目录文件
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
15 设置 view 圆角和边框
#define LRViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
16 .判断当前的iPhone设备/系统版本
//判断是否为iPhone
define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//判断是否为iPad
define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判断是否为ipod
define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
// 判断是否为 iPhone 5SE
define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
// 判断是否为iPhone 6/6s
define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
// 判断是否为iPhone 6Plus/6sPlus
define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
//获取系统版本
define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判断 iOS 8 或更高的系统版本