UI_Formatter&Function.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // UI_Formatter&Function.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/4/5.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "UI_Formatter&Function.h"
  9. @implementation UI_Formatter_Function
  10. @end
  11. //字体
  12. @implementation UIFont(scale)
  13. +(id)scaleSize:(CGFloat)font
  14. {
  15. return [UIFont systemFontOfSize:font*kSize.width / 414.0];
  16. }
  17. @end
  18. //UIButton
  19. @implementation UIButton(formatter)
  20. - (void) setImage:(UIImage *)image withTitle:(NSString *)title textColor:(UIColor*)color Font:(CGFloat)font forState:(UIControlState)stateType{
  21. [self.titleLabel setContentMode:UIViewContentModeCenter];
  22. [self.titleLabel setBackgroundColor:[UIColor clearColor]];
  23. [self setTitleColor:color forState:stateType];
  24. [self setTitle:title forState:stateType];
  25. [self.titleLabel setFont:[UIFont scaleSize:font]];
  26. [self setImage:image forState:stateType];
  27. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  28. }
  29. -(void) setTitle:(NSString*)title textColor:(UIColor*)color font:(CGFloat)font fotState:(UIControlState)stateType
  30. {
  31. [self setTitle:title forState:stateType];
  32. [self setTitleColor:color forState:stateType];
  33. [self.titleLabel setFont:[UIFont scaleSize:font]];
  34. }
  35. -(void)setTitleNormal:(NSString*)title
  36. {
  37. [self setTitle:title forState:UIControlStateNormal];
  38. }
  39. -(void)setTitleSelect:(NSString*)title
  40. {
  41. [self setTitle:title forState:UIControlStateSelected];
  42. }
  43. -(void)target:(id)obj
  44. {
  45. [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside];
  46. }
  47. -(void)target:(id)obj Tag:(NSInteger)tag
  48. {
  49. self.tag = tag;
  50. [self target:obj];
  51. }
  52. @end
  53. @implementation NSString(ex)
  54. - (NSString *)md5Encrypt
  55. {
  56. const char *cStr = [self UTF8String];
  57. unsigned char result[16];
  58. CC_MD5( cStr, (unsigned int)strlen(cStr), result );
  59. NSString* str = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  60. result[0], result[1], result[2], result[3],
  61. result[4], result[5], result[6], result[7],
  62. result[8], result[9], result[10], result[11],
  63. result[12], result[13], result[14], result[15]
  64. ];
  65. str = [self toLower:str];
  66. return str;
  67. }
  68. -(NSString *)hourAndmm
  69. {
  70. if (!self) {
  71. return @"";
  72. }
  73. int min = self.intValue;
  74. int hour = min/60;
  75. min %= 60;
  76. if (hour > 0) {
  77. return [NSString stringWithFormat:@"%d小时%d分钟",hour,min];
  78. }else{
  79. return [NSString stringWithFormat:@"%d分钟",min];
  80. }
  81. }
  82. -(NSString *)toLower:(NSString *)str
  83. {
  84. for (NSInteger i=0; i<str.length; i++) {
  85. if ([str characterAtIndex:i]>='A'&[str characterAtIndex:i]<='Z') {
  86. //A 65 a 97
  87. char temp=[str characterAtIndex:i]+32;
  88. NSRange range=NSMakeRange(i, 1);
  89. str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
  90. }
  91. }
  92. return str;
  93. }
  94. -(CGFloat)heightForWid:(CGFloat)wid Font:(CGFloat)fontSize
  95. {
  96. // if (self.length == 0) {
  97. // return 0;
  98. // }
  99. //
  100. // //这个返回没有加行间距 NSStringDrawingUsesFontLeading bylee
  101. // return [self boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size.height;
  102. if (self.length == 0) {
  103. return 0;
  104. }
  105. //NSStringDrawingUsesFontLeading这个参数是计算多行文本时用的参数
  106. CGRect rect = [self boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil];
  107. //由于计算出来的值比实际需要的值略小,故需要对其向上取整
  108. double height = ceil(rect.size.height);
  109. return height;
  110. }
  111. -(CGFloat)widthForFont:(CGFloat)fontSize
  112. {
  113. return [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size.width;
  114. }
  115. -(CGSize)sizeForFont:(CGFloat)fontSize
  116. {
  117. if (self.length < 1) {
  118. return CGSizeMake(20, 20);
  119. }
  120. return [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size;
  121. }
  122. @end
  123. @implementation UILabel(formatter)
  124. -(void)setFont:(CGFloat)font TextColor:(UIColor*)color
  125. {
  126. [self setFont:[UIFont scaleSize:font]];
  127. [self setTextColor:color];
  128. }
  129. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color
  130. {
  131. [self setText:text];
  132. [self setFont:[UIFont scaleSize:font]];
  133. [self setTextColor:color];
  134. }
  135. -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color Alignment:(NSTextAlignment)align
  136. {
  137. [self setText:text Font:font TextColor:color];
  138. [self setTextAlignment:align];
  139. }
  140. @end
  141. @implementation UIImageView(formatter)
  142. - (void)setModeAspectFill {
  143. self.contentMode = UIViewContentModeScaleAspectFill;
  144. self.layer.masksToBounds = YES;
  145. }
  146. @end
  147. @implementation UIImage(formatter)
  148. -(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
  149. {
  150. NSData *imageData = UIImageJPEGRepresentation(image, percent);
  151. UIImage *newImage = [UIImage imageWithData:imageData];
  152. return newImage;
  153. }
  154. //压缩图片尺寸
  155. - (UIImage*)scaledToSize:(CGSize)newSize
  156. {
  157. //首先要找到缩放比?
  158. CGFloat rate =newSize.width*1.0/ self.size.width ;
  159. if (self.size.height* rate > newSize.height) {
  160. //过长了。
  161. rate =newSize.height*1.0/ self.size.height ;
  162. }
  163. CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate);
  164. UIGraphicsBeginImageContext(size);
  165. [self drawInRect:CGRectMake(0,0,size.width,size.height)];
  166. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  167. UIGraphicsEndImageContext();
  168. return newImage;
  169. }
  170. - (UIImage*)scaledToWid:(CGFloat)width
  171. {
  172. //首先要找到缩放比?
  173. CGFloat rate =width*1.0/ self.size.width ;
  174. if (self.size.height* rate > width) {
  175. //过长了。
  176. rate =width*1.0/ self.size.height ;
  177. }
  178. CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate);
  179. UIGraphicsBeginImageContext(size);
  180. [self drawInRect:CGRectMake(0,0,size.width,size.height)];
  181. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  182. UIGraphicsEndImageContext();
  183. return newImage;
  184. }
  185. /**
  186. * 修改图片的大小
  187. */
  188. -(UIImage*)originImageScaleToSize:(CGSize)size
  189. {
  190. UIGraphicsBeginImageContext(size); //size 为CGSize类型,即你所需要的图片尺寸
  191. [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
  192. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  193. UIGraphicsEndImageContext();
  194. return scaledImage; //返回的就是已经改变的图片
  195. }
  196. #pragma mark - 改变照片颜色
  197. // Tint: Color
  198. -(UIImage*)tint:(UIColor*)color {
  199. return [self rt_tintedImageWithColor:color level:1.0f];
  200. }
  201. // Tint: Color + level
  202. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color level:(CGFloat)level {
  203. CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
  204. return [self rt_tintedImageWithColor:color rect:rect level:level];
  205. }
  206. // Tint: Color + Rect
  207. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect {
  208. return [self rt_tintedImageWithColor:color rect:rect level:1.0f];
  209. }
  210. // Tint: Color + Rect + level
  211. -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect level:(CGFloat)level {
  212. CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
  213. UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
  214. CGContextRef ctx = UIGraphicsGetCurrentContext();
  215. [self drawInRect:imageRect];
  216. CGContextSetFillColorWithColor(ctx, [color CGColor]);
  217. CGContextSetAlpha(ctx, level);
  218. CGContextSetBlendMode(ctx, kCGBlendModeSourceAtop);
  219. CGContextFillRect(ctx, rect);
  220. CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
  221. UIImage *darkImage = [UIImage imageWithCGImage:imageRef
  222. scale:self.scale
  223. orientation:self.imageOrientation];
  224. CGImageRelease(imageRef);
  225. UIGraphicsEndImageContext();
  226. return darkImage;
  227. }
  228. - (UIImage *)scaledAndCutToSize:(CGSize)newSize{
  229. //首先要找到缩放比 按短的适配 长的部分裁减掉
  230. CGFloat rate =newSize.width*1.0/ self.size.width;
  231. if (self.size.height* rate < newSize.height) {
  232. //过长了。
  233. rate =newSize.height*1.0/ self.size.height ;
  234. }
  235. CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate);
  236. UIGraphicsBeginImageContext(size);
  237. [self drawInRect:CGRectMake(0,0,size.width,size.height)];
  238. // Get the new image from the context
  239. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  240. // End the context
  241. UIGraphicsEndImageContext();
  242. // Return the new image.
  243. return newImage;
  244. }
  245. @end
  246. @implementation NSArray (formatter)
  247. - (id)h_safeObjectAtIndex:(NSUInteger)index{
  248. if(self.count ==0) {
  249. return (nil);
  250. }
  251. if(index >MAX(self.count -1,0)) {
  252. return (nil);
  253. }
  254. return ([self objectAtIndex:index]);
  255. }
  256. @end
  257. @implementation UIViewController(Navigation)
  258. -(void)navPushHideTabbarToVC:(UIViewController*)vc
  259. {
  260. self.tabBarController.tabBar.hidden = YES;
  261. [self.navigationController pushViewController:vc animated:YES];
  262. }
  263. -(void)goBackByNavigation{
  264. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackByNav)];
  265. [backBbi setTintColor:UIColor.whiteColor];
  266. [self.navigationItem setLeftBarButtonItem:backBbi];
  267. self.navigationController.navigationBar.translucent = NO;
  268. }
  269. -(void)goBackByNav{
  270. [self.view endEditing:1];
  271. [self.navigationController popViewControllerAnimated:YES];
  272. }
  273. //回到导航控制器的首视图
  274. -(void)goBackFirstVCByNavigation{
  275. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackFirstVCByNav)];
  276. [backBbi setTintColor:RQMianColor];
  277. [self.navigationItem setLeftBarButtonItem:backBbi];
  278. self.navigationController.navigationBar.translucent = NO;
  279. }
  280. -(void)goBackFirstVCByNav{
  281. [self.view endEditing:1];
  282. UIViewController *vc = [self.navigationController.viewControllers firstObject];
  283. [self.navigationController popToViewController:vc animated:YES];
  284. //[self.navigationController popToRootViewControllerAnimated:YES];
  285. }
  286. -(void)goBackByNavDismiss
  287. {
  288. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNav)];
  289. [backBbi setTintColor:RQMianColor];
  290. self.navigationController.navigationBar.translucent = NO;
  291. [self.navigationItem setLeftBarButtonItem:backBbi];
  292. }
  293. -(void)dismissNav
  294. {
  295. [self.view endEditing:YES];
  296. [self.navigationController dismissViewControllerAnimated:NO completion:nil];
  297. }
  298. -(void)configNavigationBarPopRoot{
  299. UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoRoot)];
  300. [backBbi setTintColor:RQMianColor];
  301. [self.navigationItem setLeftBarButtonItem:backBbi];
  302. }
  303. -(void)gotoRoot{
  304. [self.navigationController popToRootViewControllerAnimated:YES];
  305. }
  306. @end
  307. @implementation NSNumber (formatter)
  308. + (NSString *)stringValue {
  309. return [NSString stringWithFormat:@"%@",self];
  310. }
  311. @end