iOS重用宏定义
iOS 多快好省的宏(转)
原文地址:http://my.oschina.net/yongbin45/blog/150149
- // 字符串:
- #ifndef nilToEmpty
- #define nilToEmpty(object) (object!=nil)?object:@""
- #endif
- #ifndef formatStringOfObject
- #define formatStringOfObject(object) [NSString stringWithFormat:@"%@", object]
- #endif
- #ifndef nilToEmptyFormatStringOfObject
- #define nilToEmptyFormatStringOfObject(object) formatStringOfObject(nilToEmpty(object))
- #endif
- // 图片:
- #ifndef imagePath
- #define imagePath(imageName) [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]
- #endif
- // 颜色
- #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 HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0f green:((c>>8)&0xFF)/255.0f blue:(c&0xFF)/255.0f alpha:1.0f];
- // debug
- #define debug(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
- // iOS 支持
- #define SUPPORT_IPHONE_OS_VERSION(version) ( __IPHONE_OS_VERSION_MIN_REQUIRED <= version && __IPHONE_OS_VERSION_MAX_ALLOWED >= version)
- // Application delegate
- #define ApplicationDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
- // 主要单例
- #define UserDefaults [NSUserDefaults standardUserDefaults]
- #define NotificationCenter [NSNotificationCenter defaultCenter]
- #define SharedApplication [UIApplication sharedApplication]
- #define Bundle [NSBundle mainBundle]
- #define MainScreen [UIScreen mainScreen]
- // 网络指示
- #define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
- #define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
- #define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
- // 主要控件
- #define NavBar self.navigationController.navigationBar
- #define TabBar self.tabBarController.tabBar
- // 大小尺寸
- #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
- #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
- #define NavBarHeight self.navigationController.navigationBar.bounds.size.height
- #define TabBarHeight self.tabBarController.tabBar.bounds.size.height
- #define TouchHeightDefault 44.0f
- #define TouchHeightSmall 32.0f
- #define ViewWidth(v) v.frame.size.width
- #define ViewHeight(v) v.frame.size.height
- #define ViewX(v) v.frame.origin.x
- #define ViewY(v) v.frame.origin.y
- #define SelfViewWidth self.view.bounds.size.width
- #define SelfViewHeight self.view.bounds.size.height
- #define RectX(rect) rect.origin.x
- #define RectY(rect) rect.origin.y
- #define RectWidth(rect) rect.size.width
- #define RectHeight(rect) rect.size.height
- #define RectSetWidth(rect, w) CGRectMake(RectX(rect), RectY(rect), w, RectHeight(rect))
- #define RectSetHeight(rect, h) CGRectMake(RectX(rect), RectY(rect), RectWidth(rect), h)
- #define RectSetX(rect, x) CGRectMake(x, RectY(rect), RectWidth(rect), RectHeight(rect))
- #define RectSetY(rect, y) CGRectMake(RectX(rect), y, RectWidth(rect), RectHeight(rect))
- #define RectSetSize(rect, w, h) CGRectMake(RectX(rect), RectY(rect), w, h)
- #define RectSetOrigin(rect, x, y) CGRectMake(x, y, RectWidth(rect), RectHeight(rect))
- // 内存管理
- #if ! __has_feature(objc_arc)
- #define SBAutorelease(__v) ([__v autorelease]);
- #define SBReturnAutoreleased SBAutorelease
- #define SBRetain(__v) ([__v retain]);
- #define SBReturnRetained SBRetain
- #define SBRelease(__v) ([__v release]);
- #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
- #else
- // -fobjc-arc
- #define SBAutorelease(__v)
- #define SBReturnAutoreleased(__v) (__v)
- #define SBRetain(__v)
- #define SBReturnRetained(__v) (__v)
- #define SBRelease(__v)
- #if TARGET_OS_IPHONE
- // Compiling for iOS
- #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
- // iOS 6.0 or later
- #define SBDispatchQueueRelease(__v)
- #else
- // iOS 5.X or earlier
- #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
- #endif
- #else
- // Compiling for Mac OS X
- #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
- // Mac OS X 10.8 or later
- #define SBDispatchQueueRelease(__v)
- #else
- // Mac OS X 10.7 or earlier
- #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
- #endif
- #endif
- #endif
iOS开发中那些高效常用的宏(转)
原文地址:http://blog.csdn.net/duxinfeng2010/article/details/9067947
- //补充1
- //----------------------图片----------------------------
- //读取本地图片
- #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:[UIUtil imageName:_pointer]]
- //建议使用前两种宏定义,性能高于后者
- //----------------------图片----------------------------
- //补充2
- //----------------------其他----------------------------
- //方正黑体简体字体定义
- #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
- //定义一个API
- #define APIURL @"http://xxxxx/"
- //登陆API
- #define APILogin [APIURL stringByAppendingString:@"Login"]
- //设置View的tag属性
- #define VIEWWITHTAG(_OBJECT, _TAG) [_OBJECT viewWithTag : _TAG]
- //程序的本地化,引用国际化的文件
- #define MyLocal(x, ...) NSLocalizedString(x, nil)
- //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)
- //NSUserDefaults 实例化
- #define USER_DEFAULT [NSUserDefaults standardUserDefaults]
- //由角度获取弧度 有弧度获取角度
- #define degreesToRadian(x) (M_PI * (x) / 180.0)
- #define radianToDegrees(radian) (radian*180.0)/(M_PI)
- //单例化一个类
- #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; \
- }
- #endif
- //补充3
- //----------------------颜色类---------------------------
- // 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]
- #pragma mark - color functions
- #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
- #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
- //----------------------颜色类--------------------------
- //补充4
- //----------------------系统----------------------------
- //获取系统版本
- #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
- #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
- //获取当前语言
- #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
- //检查系统版本
- #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
- #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
- #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
- #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
- #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
- //----------------------系统----------------------------
iOS重用宏定义的更多相关文章
- iOS 使用宏定义函数和代码块
iOS使用宏定义函数和代码块 今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它.当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直 ...
- iOS常用宏 定义
总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...
- iOS常用宏定义--实用
在这里给大家分享一些常用的宏定义,喜欢的小伙伴可以直接在项目中使用(持续更新)!为了大家使用方便,请点击GitHub - 宏定义头文件下载 ! 1.获取屏幕宽度与高度 #define SCREEN_W ...
- iOS常用宏定义大全
宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...
- iOS之宏定义#define
最基本的宏定义用法 #define aaa bbb 表示用aaa替换bbb的内容. 宏作用范围 宏的作用范围是在当前文件内, 如果需要作用于其他类(如在类b调用类a已定义宏),那么需要在类b引入类a的 ...
- iOS常用宏定义
转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...
- iOS define 宏定义 和 const定义常量区别
const const 是c++中的修饰符. c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1. 对于co ...
- (转)iOS 常用宏定义
#ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小------------------- ...
- iOS - 常用宏定义和PCH文件知识点整理
(一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...
随机推荐
- Python flask虚拟环境安装
1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在 ...
- 10.8 wtx模拟题题解
填坑 orz w_x_c_q w_x_c_q的模拟赛(150pts,炸了) money 题目背景: 王小呆又陷入自己的梦里.(活在梦里...) 题目描述: 王小呆是一个有梦想的小菜鸡,那就是赚好多好多 ...
- 【模板】普通平衡树 Splay
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入xxx数 删除xxx数(若有多个相同的数,因只删除一个) 查询xxx数的排名(排名定义为比当前数小的数的个数 ...
- ssh 配置无密码登录
下框中在管理机上运行: [root@master ~]# ssh-keygen -t rsa #它在/root/.ssh下生成id_rsa和id_rsa.pub两个文件 [root@master ~] ...
- zabbix监控nginx进程
确认nginx有没有安装模块 然后在主站点下添加(二级站点) location /NginxStatus { stub_status on; access_log off; allow 127.0.0 ...
- Functions that return a function
javascript学习中,经常会遇到闭包的问题,然后闭包的很多例子中又会遇到很多返回函数的闭包的例子程序.因为对闭包的理解还不够透彻,然后对于Functions rerurn a function产 ...
- react 中文文档案例六 (表单)
class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...
- Android模拟器手动设置经纬度坐标
第一种方式可以在eclipse的DDMS中的Emulator control中设置,如下图 另一种是在cmd中输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不 ...
- Python十大应用领域与就业方向
参考链接:https://baijiahao.baidu.com/s?id=1604847283884842928&wfr=spider&for=pc 正文: 近些年,编程语言Pyth ...
- Codeforces Round #334(div.2)(新增不用二分代码) B
B. More Cowbell time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...