UIFormatter.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // UIFormatter.m
  3. // test11
  4. //
  5. // Created by apple on 15/11/13.
  6. // Copyright (c) 2016年 JCZ. All rights reserved.
  7. //
  8. #import "UIFormatter.h"
  9. @implementation UIFormatter
  10. @end
  11. CGRect SGRectMake(CGFloat x, CGFloat y,CGFloat width,CGFloat height){
  12. CGFloat rateX = kSize.width / 320;
  13. CGFloat rateY = kSize.height / 480;
  14. // rateY = rateX;
  15. return CGRectMake(x * rateX, y * rateY, width * rateX, height * rateY);
  16. }
  17. CGRect SGRectMakeCH(CGFloat x, CGFloat y,CGFloat width,CGFloat height){
  18. CGFloat rateX = kSize.width / 320;
  19. CGFloat rateY = kSize.height / 480;
  20. return CGRectMake(x * rateX, y * rateY, width * rateX, height * 1);
  21. }
  22. CGRect SGRectMakeCY(CGFloat x, CGFloat y,CGFloat width,CGFloat height){
  23. CGFloat rateX = kSize.width / 320;
  24. CGFloat rateY = kSize.height / 480;
  25. return CGRectMake(x * rateX, y * 1, width * rateX, height * rateY);
  26. }
  27. CGRect SGRectMakeCYH(CGFloat x, CGFloat y,CGFloat width,CGFloat height){
  28. CGFloat rateX = kSize.width / 320;
  29. // CGFloat rateY = kSize.height / 480;
  30. return CGRectMake(x * rateX, y * 1, width * rateX, height * 1);
  31. }
  32. CGSize SGSizeMake(CGFloat width,CGFloat height){
  33. CGFloat rateX = kSize.width / 320;
  34. CGFloat rateY = kSize.height / 480;
  35. return CGSizeMake(width * rateX, height * rateY);
  36. }
  37. CGFloat SGFloat(CGFloat k)
  38. {
  39. return k * kSize.height / 480;
  40. }
  41. @implementation UIViewController(Navigation)
  42. -(void)noThing{}
  43. -(void)configNavigationBarHideLeft
  44. {
  45. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(noThing)];
  46. [backBbi setTintColor:[UIColor clearColor]];
  47. [self.navigationItem setLeftBarButtonItem:backBbi];
  48. }
  49. -(void)navPushHideTabbarToVC:(UIViewController*)vc
  50. {
  51. //判断是否是homebase的子视图控制器(它的子视图控制器直接设置self.hidesBottomBarWhenPushed = YES;会无效)
  52. if ([self.parentViewController isKindOfClass:[HomeBaseVC class]]) {
  53. [self.parentViewController setHidesBottomBarWhenPushed:YES];
  54. }else{
  55. self.hidesBottomBarWhenPushed = YES;
  56. }
  57. [self.navigationController pushViewController:vc animated:YES];
  58. }
  59. -(void)configNavigationBar{
  60. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackByNavigation)];
  61. [backBbi setTintColor:defGreen];
  62. [self.navigationItem setLeftBarButtonItem:backBbi];
  63. self.navigationController.navigationBar.translucent = NO;
  64. }
  65. -(void)goBackByNavigation{
  66. [self.view endEditing:1];
  67. [self.navigationController popViewControllerAnimated:YES];
  68. }
  69. //回到导航控制器的首视图
  70. -(void)configNavigationBarFirstVC{
  71. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackFirstVCByNavigation)];
  72. [backBbi setTintColor:defGreen];
  73. [self.navigationItem setLeftBarButtonItem:backBbi];
  74. self.navigationController.navigationBar.translucent = NO;
  75. }
  76. -(void)goBackFirstVCByNavigation{
  77. [self.view endEditing:1];
  78. UIViewController *vc = [self.navigationController.viewControllers firstObject];
  79. [self.navigationController popToViewController:vc animated:YES];
  80. //[self.navigationController popToRootViewControllerAnimated:YES];
  81. }
  82. -(void)configNavigationBarDismissNav
  83. {
  84. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNav)];
  85. [backBbi setTintColor:defGreen];
  86. self.navigationController.navigationBar.translucent = NO;
  87. [self.navigationItem setLeftBarButtonItem:backBbi];
  88. }
  89. -(void)configNavigationBarPopRoot{
  90. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoRoot)];
  91. [backBbi setTintColor:defGreen];
  92. [self.navigationItem setLeftBarButtonItem:backBbi];
  93. }
  94. -(void)gotoRoot{
  95. [self.navigationController popToRootViewControllerAnimated:YES];
  96. }
  97. -(void)dismissNav
  98. {
  99. [self.view endEditing:YES];
  100. [self.navigationController dismissViewControllerAnimated:NO completion:^{
  101. [self cw_dismissViewController];
  102. }];
  103. }
  104. @end
  105. //生成带颜色的图片 可以用作按钮点击变色
  106. @implementation UIImage (Extension)
  107. + (UIImage *)imageWithColor:(UIColor *)color
  108. {
  109. CGFloat imageW = 3;
  110. CGFloat imageH = 3;
  111. // 1.开启基于位图的图形上下文
  112. UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageW, imageH), NO, 0.0);
  113. // 2.画一个color颜色的矩形框
  114. [color set];
  115. UIRectFill(CGRectMake(0, 0, imageW, imageH));
  116. // 3.拿到图片
  117. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  118. // 4.关闭上下文
  119. UIGraphicsEndImageContext();
  120. return image;
  121. }
  122. @end
  123. @implementation UIImage (scale)
  124. /**
  125. * 修改图片的大小
  126. */
  127. -(UIImage*)originImageScaleToSize:(CGSize)size
  128. {
  129. UIGraphicsBeginImageContext(size); //size 为CGSize类型,即你所需要的图片尺寸
  130. [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
  131. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  132. UIGraphicsEndImageContext();
  133. return scaledImage; //返回的就是已经改变的图片
  134. }
  135. @end
  136. @implementation UIImage(tint)
  137. // Tint: Color
  138. -(UIImage*)tint:(UIColor*)color {
  139. return [self rt_tintedImageWithColor:color level:1.0f];
  140. }
  141. // Tint: Color + level
  142. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color level:(CGFloat)level {
  143. CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
  144. return [self rt_tintedImageWithColor:color rect:rect level:level];
  145. }
  146. // Tint: Color + Rect
  147. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect {
  148. return [self rt_tintedImageWithColor:color rect:rect level:1.0f];
  149. }
  150. // Tint: Color + Rect + level
  151. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect level:(CGFloat)level {
  152. CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
  153. UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
  154. CGContextRef ctx = UIGraphicsGetCurrentContext();
  155. [self drawInRect:imageRect];
  156. CGContextSetFillColorWithColor(ctx, [color CGColor]);
  157. CGContextSetAlpha(ctx, level);
  158. CGContextSetBlendMode(ctx, kCGBlendModeSourceAtop);
  159. CGContextFillRect(ctx, rect);
  160. CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
  161. UIImage *darkImage = [UIImage imageWithCGImage:imageRef
  162. scale:self.scale
  163. orientation:self.imageOrientation];
  164. CGImageRelease(imageRef);
  165. UIGraphicsEndImageContext();
  166. return darkImage;
  167. }
  168. @end
  169. @implementation UIView(formatter)
  170. -(void)setRound
  171. {
  172. self.layer.masksToBounds = YES;
  173. self.layer.cornerRadius = self.width/2.0;
  174. }
  175. -(void)setBoardWid:(CGFloat)width Color:(UIColor*)color
  176. {
  177. [self.layer setBorderWidth:width];
  178. [self.layer setBorderColor:color.CGColor];
  179. [self.layer setMasksToBounds:YES];
  180. [self.layer setCornerRadius:6];
  181. }
  182. -(void)addViewWithRect:(CGRect)re Color:(UIColor*)color
  183. {
  184. UIView *v = [[UIView alloc] initWithFrame:re];
  185. [v setBackgroundColor:color];
  186. [self.superview addSubview:v];
  187. }
  188. -(void)addViewWithRect:(CGRect)re
  189. {
  190. [self addViewWithRect:re Color:KlineColor];
  191. }
  192. -(void)addSelfViewWithRect:(CGRect)re Color:(UIColor*)color
  193. {
  194. UIView *v = [[UIView alloc] initWithFrame:re];
  195. [v setBackgroundColor:color];
  196. [self addSubview:v];
  197. }
  198. -(void)scale:(CGFloat)rate
  199. {
  200. self.transform = CGAffineTransformScale(CGAffineTransformIdentity, rate,rate);
  201. }
  202. @end
  203. @implementation UILabel(formatter)
  204. -(void)setFont:(CGFloat)font TextColor:(UIColor*)color
  205. {
  206. [self setFont:[UIFont scaleSize:font]];
  207. [self setTextColor:color];
  208. }
  209. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color
  210. {
  211. [self setText:text];
  212. [self setFont:[UIFont scaleSize:font]];
  213. [self setTextColor:color];
  214. }
  215. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color Alignment:(NSTextAlignment)align
  216. {
  217. [self setText:text Font:font TextColor:color];
  218. [self setTextAlignment:align];
  219. }
  220. @end
  221. @implementation UIFont(scale)
  222. +(id)scaleSize:(CGFloat)font
  223. {
  224. return [UIFont systemFontOfSize:font*kSize.width / 414.0];
  225. }
  226. @end
  227. UIImage* randomPortrait()
  228. {
  229. NSString* str = [NSString stringWithFormat:@"portrait%d.png",arc4random()%16 + 1];
  230. return [UIImage imageNamed:str];
  231. }
  232. UIColor * randomColor()
  233. {
  234. UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
  235. return color;
  236. }
  237. void setRandomColor(UIView* vi)
  238. {
  239. UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
  240. [vi setBackgroundColor:color];
  241. }