NSString+Addition.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // NSString+Addition.m
  3. // PinChaoPhone
  4. //
  5. // Created by 克奎 岳 on 15/9/1.
  6. // Copyright (c) 2015年 LSY. All rights reserved.
  7. //
  8. #import "NSString+Addition.h"
  9. #define RGB_COLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
  10. @implementation NSString (Addition)
  11. - (CGFloat)commonStringWidthForFont:(CGFloat)fontSize
  12. {
  13. CGFloat width = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
  14. return width;
  15. }
  16. - (CGFloat)commonStringHeighforLabelWidth:(CGFloat)width withFontSize:(CGFloat)fontSize
  17. {
  18. CGFloat heigh = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size.height;
  19. return heigh;
  20. }
  21. - (CGPoint)commonStringLastPointWithLabelFrame:(CGRect)frame withFontSize:(CGFloat)fontSize;
  22. {
  23. CGPoint lastPoint;
  24. CGSize sz = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
  25. CGSize lineSize = [self boundingRectWithSize:CGSizeMake(frame.size.width, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
  26. if(sz.width <= lineSize.width) //判断是否折行
  27. {
  28. lastPoint = CGPointMake(frame.origin.x + sz.width, frame.origin.y);
  29. }
  30. else
  31. {
  32. lastPoint = CGPointMake(frame.origin.x + (int)sz.width % (int)lineSize.width,lineSize.height + sz.height);
  33. }
  34. return lastPoint;
  35. }
  36. //判断字符串,正责表达式
  37. - (BOOL)isUserName
  38. {
  39. NSString * regex = @"(^[A-Za-z0-9]{3,20}$)";
  40. NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  41. return [pred evaluateWithObject:self];
  42. }
  43. - (BOOL)isPassword
  44. {
  45. NSString * regex = @"(^[A-Za-z0-9]{6,20}$)";
  46. NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  47. return [pred evaluateWithObject:self];
  48. }
  49. - (BOOL)isEmail
  50. {
  51. NSString * regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  52. NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  53. return [pred evaluateWithObject:self];
  54. }
  55. - (BOOL)isUrl
  56. {
  57. NSString * regex = @"http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?";
  58. NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  59. return [pred evaluateWithObject:self];
  60. }
  61. - (BOOL)isTelephone
  62. {
  63. // NSString * MOBILE = @"^1(2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9])\\d{8}$";
  64. // NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
  65. // NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
  66. // NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
  67. // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  68. // NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  69. // NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
  70. // NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
  71. // NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
  72. // NSPredicate *regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
  73. //
  74. // return [regextestmobile evaluateWithObject:self] ||
  75. // [regextestphs evaluateWithObject:self] ||
  76. // [regextestct evaluateWithObject:self] ||
  77. // [regextestcu evaluateWithObject:self] ||
  78. // [regextestcm evaluateWithObject:self];
  79. if(!StrValid(self))
  80. {
  81. return NO;
  82. }
  83. return YES;
  84. }
  85. - (BOOL) isidentityCard
  86. {
  87. NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
  88. NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
  89. return [identityCardPredicate evaluateWithObject:self];
  90. }
  91. /**
  92. * 判断字符串是否为空
  93. *
  94. * @return BOOL
  95. */
  96. - (BOOL)isEmpty
  97. {
  98. if (self == nil || self == NULL) {
  99. return YES;
  100. }
  101. if ([self isKindOfClass:[NSNull class]]) {
  102. return YES;
  103. }
  104. if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
  105. return YES;
  106. }
  107. return NO;
  108. }
  109. // 在文本中间添加横划线
  110. - (NSMutableAttributedString *)addTextCenterLine
  111. {
  112. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self];
  113. [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, self.length)];
  114. [attrString addAttribute:NSStrikethroughColorAttributeName value:RGB_COLOR(168, 168, 170) range:NSMakeRange(0, self.length)];
  115. return attrString;
  116. }
  117. //纯数字
  118. - (BOOL)isNumber
  119. {
  120. NSString * regex = @"^[0-9]*$";
  121. NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  122. return [pred evaluateWithObject:self];
  123. }
  124. +(NSString *)showTimeStrFormDate:(NSDate*)compareDate
  125. {
  126. if( compareDate == nil ) return @"";
  127. NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow];
  128. timeInterval = -timeInterval;
  129. long temp = timeInterval;
  130. NSString *result;
  131. if (timeInterval < 60) {
  132. if( temp == 0 )
  133. result = ASLocalizedString(@"刚刚");
  134. else
  135. result = [NSString stringWithFormat:ASLocalizedString(@"%d秒前"),(int)temp];
  136. }
  137. else if(( timeInterval/60) <60){
  138. result = [NSString stringWithFormat:ASLocalizedString(@"%d分钟前"),(int)temp/60];
  139. }
  140. else if(( temp/86400) <30){
  141. NSDateFormatter *date = [[NSDateFormatter alloc] init];
  142. [date setDateFormat:@"dd"];
  143. NSString *str = [date stringFromDate:[NSDate date]];
  144. int nowday = [str intValue];
  145. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  146. [dateFormatter setDateFormat:@"dd"];
  147. NSString *strDate = [dateFormatter stringFromDate:compareDate];
  148. int day = [strDate intValue];
  149. if (nowday-day==0) {
  150. [dateFormatter setDateFormat:ASLocalizedString(@"今天 HH:mm")];
  151. result = [dateFormatter stringFromDate:compareDate];
  152. }
  153. else if(nowday-day==1)
  154. {
  155. [dateFormatter setDateFormat:ASLocalizedString(@"昨天 HH:mm")];
  156. result = [dateFormatter stringFromDate:compareDate];
  157. }
  158. else if( temp < 8 )
  159. {
  160. if (temp==1) {
  161. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  162. [dateFormatter setDateFormat:ASLocalizedString(@"昨天HH:mm")];
  163. NSString *strDate = [dateFormatter stringFromDate:compareDate];
  164. result = strDate;
  165. }
  166. else if(temp == 2)
  167. {
  168. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  169. [dateFormatter setDateFormat:ASLocalizedString(@"前天HH:mm")];
  170. NSString *strDate = [dateFormatter stringFromDate:compareDate];
  171. result = strDate;
  172. }
  173. }
  174. else
  175. {
  176. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  177. [dateFormatter setDateFormat:@"MM-dd HH:mm"];
  178. NSString *strDate = [dateFormatter stringFromDate:compareDate];
  179. result = strDate;
  180. }
  181. }
  182. else
  183. {//超过一个月的就直接显示时间了
  184. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  185. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  186. NSString *strDate = [dateFormatter stringFromDate:compareDate];
  187. result = strDate;
  188. }
  189. /*
  190. else if((temp = (temp/(3600*24))/30) <12){
  191. result = [NSString stringWithFormat:ASLocalizedString(@"%d个月前"),(int)temp];
  192. }
  193. else{
  194. temp = temp/12;
  195. result = [NSString stringWithFormat:ASLocalizedString(@"%d年前"),(int)temp];
  196. }
  197. */
  198. return result;
  199. }
  200. // 判断是不是小数2位
  201. +(BOOL)isDecimalNum:(NSString *)numStr
  202. {
  203. NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
  204. NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
  205. return [phoneTest evaluateWithObject:numStr];
  206. }
  207. @end