自用的 IOS 开发中常用的宏

2016-05-12  本文已影响112人  淡定独行

----------------获取设备信息------------------

#define kNavigationBar_HEIGHT 44 //导航条高度
#define kStatusBar_HEIGHT 20 //状态栏高度
#define kScreen_Width ([UIScreen mainScreen].bounds.size.width) //屏幕宽度
#define kScreen_Height ([UIScreen mainScreen].bounds.size.height)//屏幕高度
//判断是否 是Retina屏
#define kIsRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO) 
#define kIsPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  //是否是 iPad
//iphone4
#define kIS_IPHONE_4 ([[UIScreen mainScreen] bounds].size.height == 480.0f)
//iphone5,5C,5S
#define kIS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568.0f)
//iphone 6,6S
#define kIS_IPHONE_6 ([[UIScreen mainScreen] bounds].size.height == 667.0f)
//iphone 6 puls ,6s puls
#define kIS_IPHONE_6_PULS ([[UIScreen mainScreen] bounds].size.height == 736.0f)
//IPad Retina
#define IS_IPAD_RETINA ([[UIScreen mainScreen] bounds].size.height == 1024.0f)
//IPad pro
#define IS_IPAD_PRO ([[UIScreen mainScreen] bounds].size.height == 1366.0f)
//IPad 2, Air, Air2
#define IS_IPAD_AIR ([[UIScreen mainScreen] bounds].size.height == 1024.0f)


----------------获取系统信息------------------

//系统版本-float
#define kIOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] 
//系统版本-string
#define kCurrentSystemVersion ([[UIDevice currentDevice] systemVersion]) 
//系统语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) 

------------------IOS 版本-------------------

#define kIOS5_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"5.0"] != NSOrderedAscending )
#define kIOS6_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"6.0"] != NSOrderedAscending )
#define kIOS7_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending )
#define kIOS8_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"8.0"] != NSOrderedAscending )
#define kIOS9_OR_LATER   ( [[[UIDevice currentDevice] systemVersion] compare:@"9.0"] != NSOrderedAscending )

-----------------字体大小---------------------

#define kFont_10 [UIFont systemFontOfSize:10]
#define kFont_11 [UIFont systemFontOfSize:11]
#define kFont_12 [UIFont systemFontOfSize:12]
 #define kFont_13 [UIFont systemFontOfSize:13]
#define kFont_14 [UIFont systemFontOfSize:14]
#define kFont_15 [UIFont systemFontOfSize:15]
#define kFont_16 [UIFont systemFontOfSize:16]
#define kFont_17 [UIFont systemFontOfSize:17]
#define kFont_18 [UIFont systemFontOfSize:18]
//自定义字体大小
#define kFont(X) [UIFont systemFontOfSize:X]

-----------------RGB 转换---------------------

#define kRGBColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define kRGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

-----------------判断是否为空---------------------

#define kNotNilAndNull(_ref)  (((_ref) != nil) && (![(_ref) isEqual:[NSNull null]]))
#define kIsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))

---------------------图片资源获取---------------------

#define kImgFromBundle(file,ext )     [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
#define kImgNamed(file)         [UIImage imageNamed:file]

---------------------沙盒---------------------

//沙盒存储路径
#define kHomeDirectory [NSHomeDirectory() stringByAppendingString:@"/Documents/"]
//缓存路径
#define kApp_Caches_Path [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]

---------------------单例化一个类---------------

#define kSYNTHESIZE_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; \
}

pch 简介

pch 文件是一个标准的预编译头文件 (Pre-Compiled Header),在 Xcode6 之前的版本中,系统模板会在 Supporting Files 文件夹自动创建。但在 Xcode6 之后的版本中取消了这一文件,如果我们需要使用 pch 文件,则需要手动创建。 方法如下.

在 xcode 中添加 pch 全局引用文件的方法:

1.新建一个 pch 文件 xxxxx.pch;
2.在TARGETS下的Building Settings -> Prefix Header -> $(SRCROOT)/xxxxx.pch

注: $(SRCROOT)/xxxxx.pch 此处不是绝对,跟 pch 文件存放路径有关

补充:

1.将 Precompile Prefix Header 为 YES,预编译后的 pch 文件会被缓存起来,可以提高编译速度。
2.在 pch 文件中不要进行头文件引入,虽然这样很省事,但是牺牲的是编译速度。引入头文件可以专门建一个. h 文件进行引头。

上一篇下一篇

猜你喜欢

热点阅读