OC: NSString+MaxMethod

.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.實(shí)例化一個(gè)濾鏡
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //1.1>設(shè)置filter的默認(rèn)值
    //因?yàn)橹叭绻褂眠^濾鏡,輸入有可能會(huì)被保留,因此,在使用濾鏡之前,最好恢復(fù)默認(rèn)設(shè)置
    [filter setDefaults];
    
    //2將傳入的字符串轉(zhuǎn)換為NSData
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    //3.將NSData傳遞給濾鏡(通過KVC的方式,設(shè)置inputMessage)
    [filter setValue:data forKey:@"inputMessage"];
    
    //4.由filter輸出圖像
    CIImage *outputImage = [filter outputImage];
    
    //5.將CIImage轉(zhuǎn)換為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));
    // 創(chuàng)建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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,181評(píng)論 0 3
  • 在C語言中,五種基本數(shù)據(jù)類型存儲(chǔ)空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 4,014評(píng)論 0 2
  • ## 可重入函數(shù) ### 可重入性的理解 若一個(gè)程序或子程序可以安全的被并行執(zhí)行,則稱其為可重入的;即當(dāng)該子程序正...
    夏至亦韻閱讀 806評(píng)論 0 0
  • 櫻花飄落的時(shí)令 第七卷(全) 殘破的人生 一 悲慘生活 溫和的初春之光照耀寂靜的山脈,而這重重山連之中,沒有人知道...
    鬼少簫笛工作室閱讀 358評(píng)論 0 0
  • 轉(zhuǎn)眼間,大四開學(xué)了。整個(gè)大三暑假,簡單、平凡、無太多樂趣。 曾幾何時(shí),我對(duì)平庸恨之入骨,對(duì)安逸嗤之以鼻。而今,我卻...
    馬可的波羅閱讀 147評(píng)論 0 0

友情鏈接更多精彩內(nèi)容