UIFormatter.m 8.3 KB

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