// // NSString+Extension.m // HuaKaiChat // // Created by BigBiao on 2017/8/24. // Copyright © 2017年 huakai. All rights reserved. // #import "NSString+Extension.h" #import "CommonCrypto/CommonDigest.h" @implementation NSString (Extension) //md5加密 - (NSString *)md5{ // NSString * low=[self lowercaseString]; const char *cStr = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, strlen(cStr), result); return [[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] ] lowercaseString]; } - (NSUInteger)fileSize { // 文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; // 是否为文件夹 BOOL isDirectory = NO; // 这个路径是否存在 BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory]; // 路径不存在 if (exists == NO) return 0; // 总大小 NSUInteger totalSize = 0; if (isDirectory) { // 文件夹 // 获得文件夹中的所有内容 NSError *error = nil; NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; for (NSString *subpath in enumerator) { // 获得全路径 NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; // 获得文件属性 NSDictionary *attr = [mgr attributesOfItemAtPath:fullSubpath error:&error]; unsigned long long size = (error ? 0:[attr fileSize]); totalSize += size; } } else { // 文件 totalSize = [mgr attributesOfItemAtPath:self error:nil].fileSize; } return totalSize; } +(NSString *)dateToOld:(NSString *)birth{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *birthDay = [dateFormatter dateFromString:birth]; //获得当前系统时间 NSDate *currentDate = [NSDate date]; //获得当前系统时间与出生日期之间的时间间隔 NSTimeInterval time = [currentDate timeIntervalSinceDate:birthDay]; //时间间隔以秒作为单位,求年的话除以60*60*24*356 int age = ((int)time)/(3600*24*365); return [NSString stringWithFormat:@"%d",age]; } - (CGFloat)heightWithFont:(UIFont *)font padding:(CGFloat)padding constrainedToWidth:(CGFloat)width{ UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]]; CGSize textSize; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; paragraph.lineSpacing = padding; NSDictionary *attributes = @{NSFontAttributeName: textFont, NSParagraphStyleAttributeName: paragraph}; textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size; return ceil(textSize.height); } /** * @brief 计算文字的高度 * * @param font 字体(默认为系统字体) * @param width 约束宽度 */ - (CGFloat)heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width { UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]]; CGSize textSize; #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName: textFont, NSParagraphStyleAttributeName: paragraph}; textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size; } else { textSize = [self sizeWithFont:textFont constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; } #else NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName: textFont, NSParagraphStyleAttributeName: paragraph}; textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size; #endif return ceil(textSize.height); } /** * @brief 计算文字的宽度 * * @param font 字体(默认为系统字体) * @param height 约束高度 */ - (CGFloat)widthWithFont:(UIFont *)font constrainedToHeight:(CGFloat)height { UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]]; CGSize textSize; #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000 if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName: textFont, NSParagraphStyleAttributeName: paragraph}; textSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size; } else { textSize = [self sizeWithFont:textFont constrainedToSize:CGSizeMake(CGFLOAT_MAX, height) lineBreakMode:NSLineBreakByWordWrapping]; } #else NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName: textFont, NSParagraphStyleAttributeName: paragraph}; textSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size; #endif return ceil(textSize.width); } - (NSString *)pinYin{ if (self.length==0) { return @"#"; } if (isalpha([self characterAtIndex:0])) { return [[NSString stringWithFormat:@"%c",[self characterAtIndex:0]] uppercaseString]; } return [[NSString stringWithFormat:@"%c",pinyinFirstLetter([self characterAtIndex:0])]uppercaseString]; } - (NSString*)fullPinyin{ if ([self length] == 0) { return @""; } NSMutableString *mutableString = [NSMutableString stringWithString:self]; CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]]; return [[mutableString uppercaseString] stringByReplacingOccurrencesOfString:@"'" withString:@""]; } @end