iOS开发便捷宏收集

来源:互联网 发布:js权威指南在线看 编辑:程序博客网 时间:2024/05/17 07:38

1.快速根据RGB值创建UIColor:

#define RGBColorMake(_R_,_G_,_B_,_alpha_) [UIColor colorWithRed:_R_/255.0 green:_G_/255.0 blue:_B_/255.0 alpha:_alpha_]

使用方法:

[plain] view plaincopyprint?
  1. [_colorView setBackgroundColor:RGBColorMake(54, 137, 203, 1)];  

方便之处:宏定义中分母已有小数部分,所以不用担心除法变成取商。RGB三个字一打出来即可自动联想,让你定义颜色的时候快!快!快!

效果图:




2.快速创建NSURL宏:

[cpp] view plaincopyprint?
  1. #define DEFAULT_API_BASE @"http://www.testHost.com/Api/"  
  2. #define API_URL_MAKE(__URL__) [NSURL URLWithString:[DEFAULT_API_BASE stringByAppendingString:__URL__]]  


此宏方便之处:更改API主路径只须修改上面的宏即可。创建URL也极为方便,例如我的一个API路径是www.testHost.com/Api/Login

那么使用方法:



3,快速获取主Window及其根控制器:

[cpp] view plaincopyprint?
  1. #define MAIN_WINDOW [[[UIApplication sharedApplication]delegate]window]  
  2. #define ROOT_CONTROLLER [MAIN_WINDOW rootViewController]  

此宏方便之处:...如果要写的话要写很长呢


4.快速给view的主Layer添加阴影!

[cpp] view plaincopyprint?
  1. <p class="p1"></p><p class="p1">#define ADD_SHAW_TO_VIEW(_VIEW_,_UICOLOR_,_X_,_Y_,_ALPHA_) [_VIEW_.layer setShadowColor:_UICOLOR_.CGColor];[_VIEW_.layer setShadowOffset:CGSizeMake(_X_, _Y_)];[_VIEW_.layer setShadowOpacity:_ALPHA_]</p>  


此宏方便之处:一行代码搞定!很多人设置完阴影不是忘记设置透明度,就是忘记UIColor转CGColor,此处大快好省!一目了然要哪些参数!

效果:


5.

  1. //-------------------获取设备大小-------------------------  
  2. //NavBar高度  
  3. #define NavigationBar_HEIGHT 44  
  4. //获取屏幕 宽度、高度  
  5. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)  
  6. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)  
  7.   
  8. //-------------------获取设备大小-------------------------  

6.
  1. //----------------------图片----------------------------  
  2.   
  3. //读取本地图片  
  4. #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]  
  5.   
  6. //定义UIImage对象  
  7. #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  
  8.   
  9. //定义UIImage对象  
  10. #define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]]  
  11.   
  12. //建议使用前两种宏定义,性能高于后者  
  13. //----------------------图片---------------------------- 

0 0