iOS 常用到的宏#define

//AppDelegate

#define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication]  delegate]

//----------------------系统设备相关----------------------------

//获取设备屏幕尺寸

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//应用尺寸

#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width

#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height

//获取系统版本

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]

#define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)

#define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)

#define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)

#define isIOS7 ([[[UIDevice currentDevice] systemVersion] intValue]==7)

#define isIOS8 ([[[UIDevice currentDevice] systemVersion] intValue]==8)

#define isIOS9 ([[[UIDevice currentDevice] systemVersion] intValue]==9)

#define isIOS10 ([[[UIDevice currentDevice] systemVersion] intValue]==10)

#define isIOS11 ([[[UIDevice currentDevice] systemVersion] intValue]==11)

#define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)

#define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)

#define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)

#define isAfterIOS7 ([[[UIDevice currentDevice] systemVersion] intValue]>7)

#define isAfterIOS8 ([[[UIDevice currentDevice] systemVersion] intValue]>8)

#define isAfterIOS9 ([[[UIDevice currentDevice] systemVersion] intValue]>9)

#define isAfterIOS10 ([[[UIDevice currentDevice] systemVersion] intValue]>10)

#define isAfterIOS11 ([[[UIDevice currentDevice] systemVersion] intValue]>11)

//获取当前语言

#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

//判断是否 Retina屏、设备是否%fhone 5、是否是iPad

#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

//判断是真机还是模拟器

#if TARGET_OS_IPHONE

//iPhone Device

#endif

#if TARGET_IPHONE_SIMULATOR

//iPhone Simulator

#endif

//----------------------系统设备相关----------------------------

//----------------------内存相关----------------------------

//NSUserDefaults

#define UserDefauleSet(value,key) [[NSUserDefaults standardUserDefaults]setObject:value forKey:key];
#define UserDefauleObject(key) [[NSUserDefaults standardUserDefaults]objectIsForcedForKey:key];
#define UserDefauleRemove(key)[[NSUserDefaults standardUserDefaults]removeObjectForKey:key];

//通知
#define NotificationAdd(action,name) [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(action:) name:name object:nil];
#define NotificationPost(name,dict) [[NSNotificationCenter defaultCenter]postNotificationName:name object:nil userInfo:dict];
#define NotificationRemove(name) [[NSNotificationCenter defaultCenter]removeObserver:self name:name object:nil];
//使用ARC和不使用ARC

#if __has_feature(objc_arc)

//compiling with ARC

#else

// compiling without ARC

#endif

//释放一个对象

#define SAFE_DELETE(P) if(P) { [P release], P = nil; }

#define SAFE_RELEASE(x) [x release];x=nil

//----------------------内存相关----------------------------

//----------------------图片相关----------------------------

//读取本地图片

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

//定义UIImage对象

#define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]

//定义UIImage对象

#define ImageNamed(_pointer) [UIImage imageNamed:_pointer]

//可拉伸的图片

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

//建议使用前两种宏定义,性能高于后者

//----------------------图片相关----------------------------

//----------------------颜色相关---------------------------

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

// 获取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]

//----------------------颜色相关--------------------------

//----------------------其他----------------------------

//方正黑体简体字体定义

#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]

//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)]]

//G-C-D

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

//Alert

#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]

//由角度获取弧度 有弧度获取角度

#define degreesToRadian(x) (M_PI * (x) / 180.0)

#define radianToDegrees(radian) (radian*180.0)/(M_PI)

//----------------------其他-------------------------------

//----------------------视图相关----------------------------

//设置需要粘贴的文字或图片

#define PasteString(string)   [[UIPasteboard generalPasteboard] setString:string];

#define PasteImage(image)     [[UIPasteboard generalPasteboard] setImage:image];

//得到视图的left top的X,Y坐标点

#define VIEW_TX(view) (view.frame.origin.x)

#define VIEW_TY(view) (view.frame.origin.y)

//得到视图的right bottom的X,Y坐标点

#define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)

#define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )

//得到视图的尺寸:宽度、高度

#define VIEW_W(view)  (view.frame.size.width)

#define VIEW_H(view)  (view.frame.size.height)

//得到frame的X,Y坐标点

#define FRAME_TX(frame)  (frame.origin.x)

#define FRAME_TY(frame)  (frame.origin.y)

//得到frame的宽度、高度

#define FRAME_W(frame)  (frame.size.width)

#define FRAME_H(frame)  (frame.size.height)

//----------------------视图相关----------------------------

//---------------------打印日志--------------------------

//Debug模式下打印日志,当前行,函数名

#if DEBUG

#define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

#else

#define NSLog(FORMAT, ...) nil

#endif

//Debug模式下打印日志,当前行,函数名 并弹出一个警告

#ifdef DEBUG

#   define  WDLog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }

#else

#   define NSLog(...)

#endif

//打印Frame

#define LogFrame(frame) NSLog(@"frame[X=%.1f,Y=%.1f,W=%.1f,H=%.1f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height)

//打印Point

#define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)

//---------------------打印日志--------------------------
时间: 2024-11-07 09:53:40

iOS 常用到的宏#define的相关文章

iOS 常用宏定义

下面是在iOS开发中常用的一些宏定义: //一般的提示信息 #define mAlertView(title, msg) \ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; #define SYSTEM_VERSION_GREATER_TH

iOS开发中使用宏定义提高开发效率

iOS开发中使用宏定义提高开发效率 (2013-07-10 10:47:33) 转载▼ iOS开发中,巧妙的使用宏定义,可以提高开发效率,本篇简单介绍一下宏的定义,设置,应用,并在未来实践中不断追加一些常用的宏定义. 调试Log iPhone应用程序开发调试的时候,在代码中加入NSLog的暴力调试方法是很频繁的,但是在release的时候要删除这些调试代码,那工作量是烦躁,这样的情况下,试用宏就会显得非常的方便. 看下面的例子: #ifdef DEBUG #define LOG(...) NSL

iOS常用控件尺寸大集合

元素控件 尺寸(pts) Window(含状态栏) 320 x 480 Status Bar的高度 20 Navigation Bar的高度 44 含Prompt的Navigation Bar的高度 74 Navigation Bar的图标 20×20(透明的png) Tool Bar的高度 44 Tool Bar的图标 20×20(透明的png) Tab Bar的高度 49 Tab Bar的图标 30×30(透明的png) 竖直时键盘的高度 216.252(iOS 5+的中文键盘) 水平时键盘

iOS常用的加密算法

在iOS开发中,为了数据的安全经常对内容进行加密,在这儿我们对常用的加密算法进行了总结: 1.MD5 <span style="font-size:18px;">+ (NSString *)md5Hash:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, strlen(cStr), result ); NSString *md5Resu

iOS常用的忽略警告

在iOS开发过程中,偶尔会碰到一些编译器警告,如果能够确定该警告不会影响到程序的正常运行,则可以手动告诉编译器忽略掉这个警告 iOS常用的忽略警告类型: 1.方法弃用警告 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations"//过期的方法#pragma clang diagnostic pop 2.不兼容指针类型警告 #pragma clang diagnos

iOS 常用公共方法

iOS常用公共方法 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat size = 0.0; NSError *error; NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error]; if (error) { #ifdef DEBUG NSLog(@&quo

iOS 常用四种数据存储方式

iOS 常用四种数据存储方式 在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅, ,使得用户体验更好.下面介绍?一下数据保存的方式: 1.NSKeyedArchiver:采用归档的形式来保存数据,该数据对象需要遵守NSCoding协议,并且该对象对应的类必须提供encodeWithCoder:和initWithCoder:方法.前?一个方法告诉系统怎么对对象进行编码,而后?一个方法则是告诉系统怎么对对象进行解码.例如对Possession对

iOS.常用设计模式.02.委托模式

WTDelegate #import <Foundation/Foundation.h> @protocol WTDelegate <NSObject> @required - (void)sleep; - (void)eat; - (void)work; @end WTPhilosopher.h #import <Foundation/Foundation.h> #import "WTDelegate.h" @interface WTPhiloso

IOS常用的第三方开发库

开发几个常用的开源类库及下载地址: 1.json json编码解码2.GTMBase64 base64编码解码3.TouchXML xml解析4.SFHFKeychainUtils 安全保存用户密码到keychain中5.MBProgressHUD 很棒的一个加载等待特效框架6.ASIHTTPRequest http等相关协议封装7.EGORefreshTableHeaderView 下拉刷新代码8.AsyncImageView 异步加载图片并缓存代码9.类似setting的竖立也分栏程序 10