UIFormatter.m 8.9 KB

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