.h文件
#import <Foundation/Foundation.h>
@interface NSString (MaxMethod)
+ (CGFloat)getWidthWithText:(NSString *)text textFont:(UIFont *)textFont;
+ (CGSize)getSizeWithText:(NSString *)text needWidth:(CGFloat)needWidth textFont:(UIFont *)textFont;
+ (NSString *)stringByArray:(NSArray<NSString *> *)array andSeparatedSign:(NSString *)separatedSign;
+ (NSString *)getAfterName:(NSString *)name withSymbol:(NSString *)symbol;
+ (NSString *)getFrontName:(NSString *)name withSymbol:(NSString *)symbol;
- (NSString *)MD5;
- (UIImage *)createRRcode;
- (UIImage *)getHDRcodeWithScale:(CGFloat)scale;
- (UIImage *)drawImage;
- (BOOL)isPureFloat;
- (NSDate *)convertDateWithFormatter:(NSString *)format;
- (NSDate *)convertDateWithFormatter:(NSString *)format timeZone:(NSString *)timeZone;
@end
.m文件
#import "NSString+MaxMethod.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString (MaxMethod)
+ (CGFloat)getWidthWithText:(NSString *)text textFont:(UIFont *)textFont {
return [text sizeWithAttributes:@{NSFontAttributeName : textFont}].width;
}
+ (CGSize)getSizeWithText:(NSString *)text needWidth:(CGFloat)needWidth textFont:(UIFont *)textFont {
return [text boundingRectWithSize:CGSizeMake(needWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : textFont} context:nil].size;
}
+ (NSString *)stringByArray:(NSArray<NSString *> *)array andSeparatedSign:(NSString *)separatedSign {
if (!separatedSign) {
separatedSign = @"";
}
NSMutableString *resultString = [NSMutableString string];
for (int i = 0;i < array.count;i++) {
NSString *cityName = array[i];
if (i != array.count - 1) {
[resultString appendFormat:@"%@%@",cityName,separatedSign];
} else {
[resultString appendString:cityName];
}
}
return resultString.copy;
}
+ (NSString *)getAfterName:(NSString *)name withSymbol:(NSString *)symbol {
NSRange range = [name rangeOfString:symbol options:NSBackwardsSearch];
return [name substringFromIndex:range.location + 1];
}
+ (NSString *)getFrontName:(NSString *)name withSymbol:(NSString *)symbol {
NSRange range = [name rangeOfString:symbol options:NSBackwardsSearch];
return [name substringToIndex:range.location];
}
- (NSString *)MD5 {
const char *pointer = [self UTF8String];
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(pointer, (CC_LONG)strlen(pointer), md5Buffer);
NSMutableString *string =
[NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[string appendFormat:@"%02x", md5Buffer[i]];
return string;
}
- (UIImage *)createRRcode {
//1.实例化一个滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//1.1>设置filter的默认值
//因为之前如果使用过滤镜,输入有可能会被保留,因此,在使用滤镜之前,最好恢复默认设置
[filter setDefaults];
//2将传入的字符串转换为NSData
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
//3.将NSData传递给滤镜(通过KVC的方式,设置inputMessage)
[filter setValue:data forKey:@"inputMessage"];
//4.由filter输出图像
CIImage *outputImage = [filter outputImage];
//5.将CIImage转换为UIImage
UIImage *qrImage = [UIImage imageWithCIImage:outputImage];
//6.返回二维码图像
return qrImage;
}
- (UIImage *)getHDRcodeWithScale:(CGFloat)scale {
CIImage *image = [self createRRcode].CIImage;
CGRect extent = CGRectIntegral(image.extent);
// CGFloat scale = MIN(scale/CGRectGetWidth(extent), scale/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
- (UIImage *)drawImage {
CGFloat textWidth = [NSString getWidthWithText:self textFont:CUSFONT(9)];
CGRect rect = CGRectMake(0, 0, textWidth + 5, 20);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ref, [UIColor clearColor].CGColor);
CGContextFillRect(ref, rect);
NSMutableParagraphStyle *_style = [[NSMutableParagraphStyle alloc] init];
_style.alignment = NSTextAlignmentCenter;
[self drawInRect:CGRectMake(2.5, 5, textWidth, 10) withAttributes:@{NSFontAttributeName : CUSFONT(9), NSParagraphStyleAttributeName : _style, NSForegroundColorAttributeName : [UIColor redColor]}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
return newImage;
}
- (BOOL)isPureFloat {
NSScanner* scan = [NSScanner scannerWithString:self];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
- (NSDate *)convertDateWithFormatter:(NSString *)format {
return [self convertDateWithFormatter:format timeZone:nil];
}
- (NSDate *)convertDateWithFormatter:(NSString *)format timeZone:(NSString *)timeZone {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:format];
if (timeZone) {
[formatter setTimeZone: [NSTimeZone timeZoneWithName:timeZone]];
}
return [formatter dateFromString:self];
}
@end