iOS 开发中规范的宏定义,基本常用的宏定义整理集锦。分享给大家!

  1 #ifndef MacroDefinition_h
  2 #define MacroDefinition_h
  3 //AppDelegate
  4
  5 #define APPDELEGATE [(AppDelegate*)[UIApplication sharedApplication]  delegate]
  6 //----------------------系统设备相关----------------------------
  7 //获取设备屏幕尺寸
  8 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  9 #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//应用尺寸
 10 #define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
 11 #define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
 12 //获取系统版本
 13 #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
 14 #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
 15 #define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)
 16 #define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)
 17 #define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)
 18 #define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)
 19 #define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)
 20 #define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)
 21 //获取当前语言
 22 #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
 23
 24 //判断是否 Retina屏、设备是否%fhone 5、是否是iPad
 25 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
 26 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
 27 #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 28 //判断是真机还是模拟器
 29 #if TARGET_OS_IPHONE
 30 //iPhone Device
 31 #endif
 32 #if TARGET_IPHONE_SIMULATOR
 33 //iPhone Simulator
 34 #endif
 35 //----------------------系统设备相关----------------------------
 36
 37 //----------------------内存相关----------------------------
 38 //使用ARC和不使用ARC
 39 #if __has_feature(objc_arc)
 40 //compiling with ARC
 41 #else
 42 // compiling without ARC
 43 #endif
 44 //释放一个对象
 45 #define SAFE_DELETE(P) if(P) { [P release], P = nil; }
 46 #define SAFE_RELEASE(x) [x release];x=nil
 47 //----------------------内存相关----------------------------
 48
 49 //----------------------图片相关----------------------------
 50 //读取本地图片
 51 #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
 52 //定义UIImage对象
 53 #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
 54 //定义UIImage对象
 55 #define ImageNamed(_pointer) [UIImage imageNamed:_pointer]
 56 //可拉伸的图片
 57 #define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]
 58 #define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
 59 //建议使用前两种宏定义,性能高于后者
 60 //----------------------图片相关----------------------------
 61
 62 //----------------------颜色相关---------------------------
 63 // rgb颜色转换(16进制->10进制)
 64 #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]
 65 // 获取RGB颜色
 66 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
 67 #define RGB(r,g,b) RGBA(r,g,b,1.0f)
 68 //背景色
 69 #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
 70 //清除背景色
 71 #define CLEARCOLOR [UIColor clearColor]
 72 //----------------------颜色相关--------------------------
 73
 74 //----------------------其他----------------------------
 75 //方正黑体简体字体定义
 76 #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
 77 //file
 78 //读取文件的文本内容,默认编码为UTF-8
 79 #define FileString(name,ext)            [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]
 80 #define FileDictionary(name,ext)        [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
 81 #define FileArray(name,ext)             [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]
 82 //G-C-D
 83 #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
 84 #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
 85 //Alert
 86 #define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]
 87
 88 //由角度获取弧度 有弧度获取角度
 89 #define degreesToRadian(x) (M_PI * (x) / 180.0)
 90 #define radianToDegrees(radian) (radian*180.0)/(M_PI)
 91 //----------------------其他-------------------------------
 92
 93 //----------------------视图相关----------------------------
 94 //设置需要粘贴的文字或图片
 95 #define PasteString(string)   [[UIPasteboard generalPasteboard] setString:string];
 96 #define PasteImage(image)     [[UIPasteboard generalPasteboard] setImage:image];
 97
 98 //得到视图的left top的X,Y坐标点
 99 #define VIEW_TX(view) (view.frame.origin.x)
100 #define VIEW_TY(view) (view.frame.origin.y)
101
102 //得到视图的right bottom的X,Y坐标点
103 #define VIEW_BX(view) (view.frame.origin.x + view.frame.size.width)
104 #define VIEW_BY(view) (view.frame.origin.y + view.frame.size.height )
105
106 //得到视图的尺寸:宽度、高度
107 #define VIEW_W(view)  (view.frame.size.width)
108 #define VIEW_H(view)  (view.frame.size.height)
109 //得到frame的X,Y坐标点
110 #define FRAME_TX(frame)  (frame.origin.x)
111 #define FRAME_TY(frame)  (frame.origin.y)
112 //得到frame的宽度、高度
113 #define FRAME_W(frame)  (frame.size.width)
114 #define FRAME_H(frame)  (frame.size.height)
115 //----------------------视图相关----------------------------
116
117 //---------------------打印日志--------------------------
118 //Debug模式下打印日志,当前行,函数名
119 #if DEBUG
120 #define DLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
121 #else
122 #define NSLog(FORMAT, ...) nil
123 #endif
124 //Debug模式下打印日志,当前行,函数名 并弹出一个警告
125 #ifdef DEBUG
126 #   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]; }
127 #else
128 #   define NSLog(...)
129 #endif
130 //打印Frame
131 #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)
132 //打印Point
133 #define LogPoint(point) NSLog(@"Point[X=%.1f,Y=%.1f]",point.x,point.y)
134 //---------------------打印日志--------------------------
135 #endif
时间: 2024-08-25 16:23:34

iOS 开发中规范的宏定义,基本常用的宏定义整理集锦。分享给大家!的相关文章

iOS开发中 Mac OS X 及Xcode 常用快捷键

版权声明:本文为博主原创文章,转载请注明出处,仅用于学习交流和资源共享.关注新浪微博:极客James 在学习iOS开发之前要熟悉OS X 系统以及开发工具XCode的使用,作为程序员不用鼠标是装B的首选,下面就来介绍下常见的操作,每一个快捷键都可以试一试 常用快捷键:  Xcode常见的快捷键    

IOS开发中针对UIImageView的几种常用手势

// //  ViewController.m //  05-手势 // //  Created by wanghy on 15/9/21. //  Copyright (c) 2015年 wanghy. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView* imageView; @

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

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

IOS开发中常用的宏定义

有些时候,我们需要将代码简洁化,这样便于读代码.我们可以将一些不变的东东抽取出来,将变化的东西作为参数.定义为宏,这样在写的时候就简单多了. 下面例举了一些常用的宏定义和大家分享: 1. 判断设备的操作系统是不是ios7 #define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] ) 2. 判断当前设备是不是iPhone5 #define kScreenIphone5 (([[UIScreen main

IOS开发中常量的处理

IOS开发中,文本解析,键值处理等都会用到常量 项目中大部分的处理都是用的C的预处理#define来处理常量 ,比如: #define ELEMENT “element” #define NODE "node" 复制代码 等到用的时候,直接加入这个头文件就可以了. 但是有问题,因为这样定义出来的常量值是c字符串,不能直接使用OBJC中NSString类中提供的方法,很是不方便,所以博主写了可以这样做: #import extern NSString * const kInitURL;

iOS开发中的那些的约定俗成(1)————《编写高质量iOS与OS X代码的52个有效方法》读书笔记(第一章)

iOS开发中的那些的约定俗成(1) ----<编写高质量iOS与OS X代码的52个有效方法>读书笔记(第一章) 前言 "我要成为一个高产的开发人员.""想要混的好,就得多努力." 写这些东西是因为毕竟看了书,但是看书看过去之后,也许印象不是很深刻,有些东西现在也理解不了,那我就把我理解的,现在就可以用到的东西,简单的写出来就好,让自己今后看到就能明白其中的意思. 还有就是锻炼一下表达,编辑能力,慢慢的提升自己,随时随地的都要有一个锻炼的心. 最后当然就

深入理解 iOS 开发中的锁

来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89474/ 点击 → 申请加入伯乐在线专栏作者 摘要 本文的目的不是介绍 iOS 中各种锁如何使用,一方面笔者没有大量的实战经验,另一方面这样的文章相当多,比如 iOS中保证线程安全的几种方式与性能对比.iOS 常见知识点(三):Lock.本文也不会详细介绍锁的具体实现原理,这会涉及到太多相关知识,笔者不敢误人子弟. 本文要做的就是简单的分析 iOS 开发中常见的几种锁如何实现,以及优缺点是什么,为什么会有性能上

iOS开发中一些有用的小代码

1.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@&qu

iOS开发中打电话发短信等功能的实现

在APP开发中,可能会涉及到打电话.发短信.发邮件等功能.比如说,通常一个产品的“关于”页面,会有开发者的联系方式,理想情况下,当用户点击该电话号码时,能够自动的帮用户拨出去,就涉及到了打电话的功能. iOS开发中,有三种方式可以打电话: (1)直接跳到拨号界面,代码如下 1 2 NSURL *url = [NSURL URLWithString:@"tel://10010"];  [[UIApplication sharedApplication] openURL:url]; 缺点: