// // 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 (ex) +(id)scaleSize:(CGFloat)font { return [UIFont systemFontOfSize:font*kSize.width / 414.0]; } @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 NSNumber (formatter) + (NSString *)stringValue { return [NSString stringWithFormat:@"%@",self]; } @end @implementation UIView (formatter) - (void)setX:(CGFloat)x { CGRect rect = self.frame; rect.origin.x = x; self.frame = rect; } -(CGFloat)x { return self.frame.origin.x; } - (void)setY:(CGFloat)y { CGRect rect = self.frame; rect.origin.y = y; self.frame = rect; } -(CGFloat)y { return self.frame.origin.y; } - (void)setCenterX:(CGFloat)centerX { CGPoint center = self.center; center.x = centerX; self.center = center; } - (CGFloat)centerX { return self.center.x; } - (void)setCenterY:(CGFloat)centerY { CGPoint center = self.center; center.y = centerY; self.center = center; } - (CGFloat)centerY { return self.center.y; } - (void)setWidth:(CGFloat)width { CGRect rect = self.frame; rect.size.width = width; self.frame = rect; } - (CGFloat)width { return self.frame.size.width; } - (void)setHeight:(CGFloat)height { CGRect rect = self.frame; rect.size.height = height; self.frame = rect; } - (CGFloat)height { return self.frame.size.height; } @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