Last active
August 29, 2015 13:57
-
-
Save YauzZ/9499293 to your computer and use it in GitHub Desktop.
各种iOS常用代码段
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
macro.h | |
使用方法: | |
*需要时import | |
*添加到-Prefix.pch文件中,所有类中都可以使用 | |
author : YauzZ | |
Date : 2014-03-12 | |
*/ | |
// 对NSLog的格式化宏 | |
#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 | |
// RGB颜色 | |
#define Color_RGB(RED,GREEN,BLUE) [UIColor colorWithRed:RED/255.00 green:GREEN/255.00 blue:BLUE/255.00 alpha:1.0] | |
// 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_RGBA(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] | |
// 方正黑体简体字体定义 | |
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F] | |
//单例化一个类 | |
#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; \ | |
} | |
// 去除"-(id)performSelector:(SEL)aSelector withObject:(id)object;"的警告 | |
#define SuppressPerformSelectorLeakWarning(Stuff) \ | |
do { \ | |
_Pragma("clang diagnostic push") \ | |
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ | |
Stuff; \ | |
_Pragma("clang diagnostic pop") \ | |
} while (0) | |
// documents structure of application | |
#define APP_DOCUMENT [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_LIBRARY [NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_CACHES_PATH [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] | |
#define APP_USERINFO_PATH userInfoPath() | |
//------------------------------------Debug/Release | |
#ifdef DEBUG | |
//Debug模式 | |
//... | |
#else | |
//发布模式 | |
//... | |
//屏蔽NSLog | |
#define NSLog(...) {}; | |
#endif | |
//------------------------------------Simulator/Device | |
//区分模拟器和真机 | |
#if TARGET_OS_IPHONE | |
//iPhone Device | |
#endif | |
#if TARGET_IPHONE_SIMULATOR | |
//iPhone Simulator | |
#endif | |
//------------------------------------ARC/no RAC | |
//ARC | |
#if __has_feature(objc_arc) | |
//compiling with ARC | |
#else | |
// compiling without ARC | |
#endif | |
// 异常抛出 | |
#define Exception(NAME,REASON) \ | |
NSException *exception = [NSException exceptionWithName:NAME reason:REASON userInfo:nil]; \ | |
@throw exception; | |
// ARC和MRC的适配 | |
#if __has_feature(objc_arc) | |
#define SAFE_ARC_PROP_RETAIN strong | |
#define SAFE_ARC_RETAIN(x) (x) | |
#define SAFE_ARC_RELEASE(x) | |
#define SAFE_ARC_AUTORELEASE(x) (x) | |
#define SAFE_ARC_BLOCK_COPY(x) (x) | |
#define SAFE_ARC_BLOCK_RELEASE(x) | |
#define SAFE_ARC_SUPER_DEALLOC() | |
#define SAFE_ARC_AUTORELEASE_POOL_START() @autoreleasepool { | |
#define SAFE_ARC_AUTORELEASE_POOL_END() } | |
#else | |
#define SAFE_ARC_PROP_RETAIN retain | |
#define SAFE_ARC_RETAIN(x) ([(x) retain]) | |
#define SAFE_ARC_RELEASE(x) ([(x) release]) | |
#define SAFE_ARC_AUTORELEASE(x) ([(x) autorelease]) | |
#define SAFE_ARC_BLOCK_COPY(x) (Block_copy(x)) | |
#define SAFE_ARC_BLOCK_RELEASE(x) (Block_release(x)) | |
#define SAFE_ARC_SUPER_DEALLOC() ([super dealloc]) | |
#define SAFE_ARC_AUTORELEASE_POOL_START() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
#define SAFE_ARC_AUTORELEASE_POOL_END() [pool release]; | |
#endif | |
//Block | |
typedef void(^VoidBlock)(); | |
typedef BOOL(^BoolBlock)(); | |
typedef int (^IntBlock) (); | |
typedef id (^IDBlock) (); | |
typedef void(^VoidBlock_int)(int); | |
typedef BOOL(^BoolBlock_int)(int); | |
typedef int (^IntBlock_int) (int); | |
typedef id (^IDBlock_int) (int); | |
typedef void(^VoidBlock_string)(NSString*); | |
typedef BOOL(^BoolBlock_string)(NSString*); | |
typedef int (^IntBlock_string) (NSString*); | |
typedef id (^IDBlock_string) (NSString*); | |
typedef void(^VoidBlock_id)(id); | |
typedef BOOL(^BoolBlock_id)(id); | |
typedef int (^IntBlock_id) (id); | |
typedef id (^IDBlock_id) (id); | |
//System | |
#define PasteString(string) [[UIPasteboard generalPasteboard] setString:string]; | |
#define PasteImage(image) [[UIPasteboard generalPasteboard] setImage:image]; | |
//Image | |
//可拉伸的图片 | |
#define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)] | |
#define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode] | |
//file | |
//读取文件的文本内容,默认编码为UTF-8 | |
#define FileString(name,ext) [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil] | |
#define FileDictionary(name,ext) [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]] | |
#define FileArray(name,ext) [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]] | |
//数学 | |
#define PI 3.14159 | |
//输出frame(frame是结构体,没法%@) | |
#define LOGFRAME(f) NSLog(@"\nx:%f\ny:%f\nwidth:%f\nheight:%f\n",f.origin.x,f.origin.y,f.size.width,f.size.height) | |
#define LOGBOOL(b) NSLog(@"%@",b?@"YES":@"NO"); | |
//弹出信息 | |
#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show] | |
//App | |
#define kApp ((AppDelegate *)[UIApplication sharedApplication].delegate) | |
#define kNav ((AppDelegate *)[UIApplication sharedApplication].delegate.navigationController) | |
//color | |
#define RGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0] | |
#define RGBAlpha(r, g, b, a) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)] | |
#define HexRGB(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 HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)] | |
//转换 | |
#define I2S(number) [NSString stringWithFormat:@"%d",number] | |
#define F2S(number) [NSString stringWithFormat:@"%f",number] | |
#define DATE(stamp) [NSDate dateWithTimeIntervalSince1970:[stamp intValue]]; | |
//设备屏幕尺寸 | |
#define kScreen_Height ([UIScreen mainScreen].bounds.size.height) | |
#define kScreen_Width ([UIScreen mainScreen].bounds.size.width) | |
#define kScreen_Frame (CGRectMake(0, 0 ,kScreen_Width,kScreen_Height)) | |
#define kScreen_CenterX kScreen_Width/2 | |
#define kScreen_CenterY kScreen_Height/2 | |
//应用尺寸(不包括状态栏,通话时状态栏高度不是20,所以需要知道具体尺寸) | |
#define kContent_Height ([UIScreen mainScreen].applicationFrame.size.height) | |
#define kContent_Width ([UIScreen mainScreen].applicationFrame.size.width) | |
#define kContent_Frame (CGRectMake(0, 0 ,kContent_Width,kContent_Height)) | |
#define kContent_CenterX kContent_Width/2 | |
#define kContent_CenterY kContent_Height/2 | |
/* | |
类似九宫格的九个点 | |
p1 --- p2 --- p3 | |
| | | | |
p4 --- p5 --- p6 | |
| | | | |
p7 --- p8 --- p9 | |
*/ | |
#define kP1 CGPointMake(0 ,0) | |
#define kP2 CGPointMake(kContent_Width/2 ,0) | |
#define kP3 CGPointMake(kContent_Width ,0) | |
#define kP4 CGPointMake(0 ,kContent_Height/2) | |
#define kP5 CGPointMake(kContent_Width/2 ,kContent_Height/2) | |
#define kP6 CGPointMake(kContent_Width ,kContent_Height/2) | |
#define kP7 CGPointMake(0 ,kContent_Height) | |
#define kP8 CGPointMake(kContent_Width/2 ,kContent_Height) | |
#define kP9 CGPointMake(kContent_Width ,kContent_Height) | |
//********************************************* | |
//GCD | |
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block) | |
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block) | |
//Device | |
#define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4) | |
#define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5) | |
#define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6) | |
#define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4) | |
#define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5) | |
#define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6) | |
#define iOS ([[[UIDevice currentDevice] systemVersion] floatValue]) | |
#define isRetina ([[UIScreen mainScreen] scale]==2) | |
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) | |
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) | |
//拨打电话 | |
#define canTel ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:"]]) | |
#define tel(phoneNumber) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]]]) | |
#define telprompt(phoneNumber) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phoneNumber]]]) | |
//打开URL | |
#define canOpenURL(appScheme) ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appScheme]]) | |
#define openURL(appScheme) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:appScheme]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment