NSString+Extension.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // NSString+Extension.m
  3. // HuaKaiChat
  4. //
  5. // Created by BigBiao on 2017/8/24.
  6. // Copyright © 2017年 huakai. All rights reserved.
  7. //
  8. #import "NSString+Extension.h"
  9. #import "CommonCrypto/CommonDigest.h"
  10. @implementation NSString (Extension)
  11. //md5加密
  12. - (NSString *)md5{
  13. // NSString * low=[self lowercaseString];
  14. const char *cStr = [self UTF8String];
  15. unsigned char result[CC_MD5_DIGEST_LENGTH];
  16. CC_MD5(cStr, strlen(cStr), result);
  17. return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  18. result[0], result[1], result[2], result[3],
  19. result[4], result[5], result[6], result[7],
  20. result[8], result[9], result[10], result[11],
  21. result[12], result[13], result[14], result[15]
  22. ] lowercaseString];
  23. }
  24. - (NSUInteger)fileSize
  25. {
  26. // 文件管理者
  27. NSFileManager *mgr = [NSFileManager defaultManager];
  28. // 是否为文件夹
  29. BOOL isDirectory = NO;
  30. // 这个路径是否存在
  31. BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
  32. // 路径不存在
  33. if (exists == NO) return 0;
  34. // 总大小
  35. NSUInteger totalSize = 0;
  36. if (isDirectory) { // 文件夹
  37. // 获得文件夹中的所有内容
  38. NSError *error = nil;
  39. NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
  40. for (NSString *subpath in enumerator) {
  41. // 获得全路径
  42. NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
  43. // 获得文件属性
  44. NSDictionary *attr = [mgr attributesOfItemAtPath:fullSubpath error:&error];
  45. unsigned long long size = (error ? 0:[attr fileSize]);
  46. totalSize += size;
  47. }
  48. } else { // 文件
  49. totalSize = [mgr attributesOfItemAtPath:self error:nil].fileSize;
  50. }
  51. return totalSize;
  52. }
  53. +(NSString *)dateToOld:(NSString *)birth{
  54. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
  55. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  56. NSDate *birthDay = [dateFormatter dateFromString:birth];
  57. //获得当前系统时间
  58. NSDate *currentDate = [NSDate date];
  59. //获得当前系统时间与出生日期之间的时间间隔
  60. NSTimeInterval time = [currentDate timeIntervalSinceDate:birthDay];
  61. //时间间隔以秒作为单位,求年的话除以60*60*24*356
  62. int age = ((int)time)/(3600*24*365);
  63. return [NSString stringWithFormat:@"%d",age];
  64. }
  65. - (CGFloat)heightWithFont:(UIFont *)font padding:(CGFloat)padding constrainedToWidth:(CGFloat)width{
  66. UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
  67. CGSize textSize;
  68. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  69. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  70. paragraph.lineSpacing = padding;
  71. NSDictionary *attributes = @{NSFontAttributeName: textFont,
  72. NSParagraphStyleAttributeName: paragraph};
  73. textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
  74. options:(NSStringDrawingUsesLineFragmentOrigin |
  75. NSStringDrawingTruncatesLastVisibleLine)
  76. attributes:attributes
  77. context:nil].size;
  78. return ceil(textSize.height);
  79. }
  80. /**
  81. * @brief 计算文字的高度
  82. *
  83. * @param font 字体(默认为系统字体)
  84. * @param width 约束宽度
  85. */
  86. - (CGFloat)heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width
  87. {
  88. UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
  89. CGSize textSize;
  90. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
  91. if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
  92. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  93. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  94. NSDictionary *attributes = @{NSFontAttributeName: textFont,
  95. NSParagraphStyleAttributeName: paragraph};
  96. textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
  97. options:(NSStringDrawingUsesLineFragmentOrigin |
  98. NSStringDrawingTruncatesLastVisibleLine)
  99. attributes:attributes
  100. context:nil].size;
  101. } else {
  102. textSize = [self sizeWithFont:textFont
  103. constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
  104. lineBreakMode:NSLineBreakByWordWrapping];
  105. }
  106. #else
  107. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  108. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  109. NSDictionary *attributes = @{NSFontAttributeName: textFont,
  110. NSParagraphStyleAttributeName: paragraph};
  111. textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
  112. options:(NSStringDrawingUsesLineFragmentOrigin |
  113. NSStringDrawingTruncatesLastVisibleLine)
  114. attributes:attributes
  115. context:nil].size;
  116. #endif
  117. return ceil(textSize.height);
  118. }
  119. /**
  120. * @brief 计算文字的宽度
  121. *
  122. * @param font 字体(默认为系统字体)
  123. * @param height 约束高度
  124. */
  125. - (CGFloat)widthWithFont:(UIFont *)font constrainedToHeight:(CGFloat)height
  126. {
  127. UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
  128. CGSize textSize;
  129. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
  130. if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
  131. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  132. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  133. NSDictionary *attributes = @{NSFontAttributeName: textFont,
  134. NSParagraphStyleAttributeName: paragraph};
  135. textSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height)
  136. options:(NSStringDrawingUsesLineFragmentOrigin |
  137. NSStringDrawingTruncatesLastVisibleLine)
  138. attributes:attributes
  139. context:nil].size;
  140. } else {
  141. textSize = [self sizeWithFont:textFont
  142. constrainedToSize:CGSizeMake(CGFLOAT_MAX, height)
  143. lineBreakMode:NSLineBreakByWordWrapping];
  144. }
  145. #else
  146. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
  147. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  148. NSDictionary *attributes = @{NSFontAttributeName: textFont,
  149. NSParagraphStyleAttributeName: paragraph};
  150. textSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height)
  151. options:(NSStringDrawingUsesLineFragmentOrigin |
  152. NSStringDrawingTruncatesLastVisibleLine)
  153. attributes:attributes
  154. context:nil].size;
  155. #endif
  156. return ceil(textSize.width);
  157. }
  158. - (NSString *)pinYin{
  159. if (self.length==0) {
  160. return @"#";
  161. }
  162. if (isalpha([self characterAtIndex:0])) {
  163. return [[NSString stringWithFormat:@"%c",[self characterAtIndex:0]] uppercaseString];
  164. }
  165. return [[NSString stringWithFormat:@"%c",pinyinFirstLetter([self characterAtIndex:0])]uppercaseString];
  166. }
  167. - (NSString*)fullPinyin{
  168. if ([self length] == 0)
  169. {
  170. return @"";
  171. }
  172. NSMutableString *mutableString = [NSMutableString stringWithString:self];
  173. CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
  174. mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
  175. return [[mutableString uppercaseString] stringByReplacingOccurrencesOfString:@"'" withString:@""];
  176. }
  177. @end