// // UI_Formatter&Function.m // LN_School // // Created by apple on 2017/4/5. // Copyright © 2017年 Danson. All rights reserved. // #import "UI_Formatter&Function.h" @implementation UI_Formatter_Function @end //字体 @implementation UIFont(scale) +(id)scaleSize:(CGFloat)font { return [UIFont systemFontOfSize:font*kSize.width / 414.0]; } @end //UIButton @implementation UIButton(formatter) - (void) setImage:(UIImage *)image withTitle:(NSString *)title textColor:(UIColor*)color Font:(CGFloat)font forState:(UIControlState)stateType{ [self.titleLabel setContentMode:UIViewContentModeCenter]; [self.titleLabel setBackgroundColor:[UIColor clearColor]]; [self setTitleColor:color forState:stateType]; [self setTitle:title forState:stateType]; [self.titleLabel setFont:[UIFont scaleSize:font]]; [self setImage:image forState:stateType]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; } -(void) setTitle:(NSString*)title textColor:(UIColor*)color font:(CGFloat)font fotState:(UIControlState)stateType { [self setTitle:title forState:stateType]; [self setTitleColor:color forState:stateType]; [self.titleLabel setFont:[UIFont scaleSize:font]]; } -(void)setTitleNormal:(NSString*)title { [self setTitle:title forState:UIControlStateNormal]; } -(void)setTitleSelect:(NSString*)title { [self setTitle:title forState:UIControlStateSelected]; } -(void)target:(id)obj { [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside]; } -(void)target:(id)obj Tag:(NSInteger)tag { self.tag = tag; [self target:obj]; } @end @implementation NSString(ex) - (NSString *)md5Encrypt { const char *cStr = [self UTF8String]; unsigned char result[16]; CC_MD5( cStr, (unsigned int)strlen(cStr), result ); NSString* str = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; str = [self toLower:str]; return str; } -(NSString *)hourAndmm { if (!self) { return @""; } int min = self.intValue; int hour = min/60; min %= 60; if (hour > 0) { return [NSString stringWithFormat:@"%d小时%d分钟",hour,min]; }else{ return [NSString stringWithFormat:@"%d分钟",min]; } } -(NSString *)toLower:(NSString *)str { for (NSInteger i=0; i='A'&[str characterAtIndex:i]<='Z') { //A 65 a 97 char temp=[str characterAtIndex:i]+32; NSRange range=NSMakeRange(i, 1); str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]]; } } return str; } -(CGFloat)heightForWid:(CGFloat)wid Font:(CGFloat)fontSize { // if (self.length == 0) { // return 0; // } // // //这个返回没有加行间距 NSStringDrawingUsesFontLeading bylee // return [self boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size.height; if (self.length == 0) { return 0; } //NSStringDrawingUsesFontLeading这个参数是计算多行文本时用的参数 CGRect rect = [self boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil]; //由于计算出来的值比实际需要的值略小,故需要对其向上取整 double height = ceil(rect.size.height); return height; } -(CGFloat)widthForFont:(CGFloat)fontSize { return [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size.width; } -(CGSize)sizeForFont:(CGFloat)fontSize { if (self.length < 1) { return CGSizeMake(20, 20); } return [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont scaleSize:fontSize]} context:nil].size; } @end @implementation UILabel(formatter) -(void)setFont:(CGFloat)font TextColor:(UIColor*)color { [self setFont:[UIFont scaleSize:font]]; [self setTextColor:color]; } -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color { [self setText:text]; [self setFont:[UIFont scaleSize:font]]; [self setTextColor:color]; } -(void)setText:(NSString*)text Font:(CGFloat)font TextColor:(UIColor*)color Alignment:(NSTextAlignment)align { [self setText:text Font:font TextColor:color]; [self setTextAlignment:align]; } @end @implementation UIImageView(formatter) - (void)setModeAspectFill { self.contentMode = UIViewContentModeScaleAspectFill; self.layer.masksToBounds = YES; } @end @implementation UIImage(formatter) -(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 - (UIImage*)scaledToSize:(CGSize)newSize { //首先要找到缩放比? CGFloat rate =newSize.width*1.0/ self.size.width ; if (self.size.height* rate > newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0,0,size.width,size.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } - (UIImage*)scaledToWid:(CGFloat)width { //首先要找到缩放比? CGFloat rate =width*1.0/ self.size.width ; if (self.size.height* rate > width) { //过长了。 rate =width*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0,0,size.width,size.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } /** * 修改图片的大小 */ -(UIImage*)originImageScaleToSize:(CGSize)size { UIGraphicsBeginImageContext(size); //size 为CGSize类型,即你所需要的图片尺寸 [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; //返回的就是已经改变的图片 } #pragma mark - 改变照片颜色 // Tint: Color -(UIImage*)tint:(UIColor*)color { return [self rt_tintedImageWithColor:color level:1.0f]; } // Tint: Color + level -(UIImage*)rt_tintedImageWithColor:(UIColor*)color level:(CGFloat)level { CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height); return [self rt_tintedImageWithColor:color rect:rect level:level]; } // Tint: Color + Rect -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect { return [self rt_tintedImageWithColor:color rect:rect level:1.0f]; } // Tint: Color + Rect + level -(UIImage*)rt_tintedImageWithColor:(UIColor*)color rect:(CGRect)rect level:(CGFloat)level { CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height); UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); [self drawInRect:imageRect]; CGContextSetFillColorWithColor(ctx, [color CGColor]); CGContextSetAlpha(ctx, level); CGContextSetBlendMode(ctx, kCGBlendModeSourceAtop); CGContextFillRect(ctx, rect); CGImageRef imageRef = CGBitmapContextCreateImage(ctx); UIImage *darkImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation]; CGImageRelease(imageRef); UIGraphicsEndImageContext(); return darkImage; } - (UIImage *)scaledAndCutToSize:(CGSize)newSize{ //首先要找到缩放比 按短的适配 长的部分裁减掉 CGFloat rate =newSize.width*1.0/ self.size.width; if (self.size.height* rate < newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0,0,size.width,size.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } @end @implementation NSArray (formatter) - (id)h_safeObjectAtIndex:(NSUInteger)index{ if(self.count ==0) { return (nil); } if(index >MAX(self.count -1,0)) { return (nil); } return ([self objectAtIndex:index]); } @end @implementation UIViewController(Navigation) -(void)navPushHideTabbarToVC:(UIViewController*)vc { self.tabBarController.tabBar.hidden = YES; [self.navigationController pushViewController:vc animated:YES]; } -(void)goBackByNavigation{ UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackByNav)]; [backBbi setTintColor:UIColor.whiteColor]; [self.navigationItem setLeftBarButtonItem:backBbi]; self.navigationController.navigationBar.translucent = NO; } -(void)goBackByNav{ [self.view endEditing:1]; [self.navigationController popViewControllerAnimated:YES]; } //回到导航控制器的首视图 -(void)goBackFirstVCByNavigation{ UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackFirstVCByNav)]; [backBbi setTintColor:RQMianColor]; [self.navigationItem setLeftBarButtonItem:backBbi]; self.navigationController.navigationBar.translucent = NO; } -(void)goBackFirstVCByNav{ [self.view endEditing:1]; UIViewController *vc = [self.navigationController.viewControllers firstObject]; [self.navigationController popToViewController:vc animated:YES]; //[self.navigationController popToRootViewControllerAnimated:YES]; } -(void)goBackByNavDismiss { UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNav)]; [backBbi setTintColor:RQMianColor]; self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBbi]; } -(void)dismissNav { [self.view endEditing:YES]; [self.navigationController dismissViewControllerAnimated:NO completion:nil]; } -(void)configNavigationBarPopRoot{ UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoRoot)]; [backBbi setTintColor:RQMianColor]; [self.navigationItem setLeftBarButtonItem:backBbi]; } -(void)gotoRoot{ [self.navigationController popToRootViewControllerAnimated:YES]; } @end @implementation NSNumber (formatter) + (NSString *)stringValue { return [NSString stringWithFormat:@"%@",self]; } @end