TextUtils.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // TextUtils.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/20/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "TextUtils.h"
  9. @implementation TextUtils
  10. + (long)countOccurencesOfString:(NSString*)string find:(NSString*)searchString {
  11. long strCount = string.length - [[string stringByReplacingOccurrencesOfString:searchString withString:@""] length];
  12. return strCount / [searchString length];
  13. }
  14. + (NSString *)formatNumber:(NSString *)mobileNumber
  15. {
  16. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
  17. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
  18. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
  19. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
  20. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
  21. NSLog(@"%@", mobileNumber);
  22. int length = (int)[mobileNumber length];
  23. if(length > 10)
  24. {
  25. mobileNumber = [mobileNumber substringFromIndex: length-10];
  26. NSLog(@"%@", mobileNumber);
  27. }
  28. return mobileNumber;
  29. }
  30. + (int)getLength:(NSString *)mobileNumber
  31. {
  32. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
  33. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
  34. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
  35. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
  36. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
  37. int length = (int)[mobileNumber length];
  38. return length;
  39. }
  40. +(NSString*) encodePhoneNumber:(NSString*) phone
  41. {
  42. NSLocale* locale = [NSLocale currentLocale];
  43. if( [[locale localeIdentifier] compare:@"en_US"]!=NSOrderedSame )
  44. return phone;
  45. if( [phone length]==0 )
  46. return phone;
  47. if( [phone rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] ].location==NSNotFound )
  48. {
  49. const char* string = [phone UTF8String];
  50. int length = [phone lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  51. if( [[NSString stringWithCString:string length:1] intValue]==1 )
  52. {
  53. if( length > 11 )
  54. return phone;
  55. NSMutableData* array = [[NSMutableData alloc] init] ;
  56. // 1 (234) 567-8901
  57. for( int i=0; i < length; i++ )
  58. {
  59. if( i==1 )
  60. {
  61. [array appendBytes:" (" length:2];
  62. }
  63. if( i == 4 )
  64. {
  65. [array appendBytes:") " length:2];
  66. }
  67. if( i==7 )
  68. {
  69. [array appendBytes:" " length:1];
  70. }
  71. [array appendBytes:string++ length:1];
  72. }
  73. return [NSString stringWithUTF8String:(const char*)[array bytes]];
  74. }
  75. else
  76. {
  77. // (012) 345-6789
  78. if( length > 10 )
  79. return phone;
  80. NSMutableData* array = [[NSMutableData alloc] init] ;
  81. int i=0;
  82. if( length <=7 )
  83. {
  84. for( i=0; i < length; i++ )
  85. {
  86. if( i==3 )
  87. {
  88. [array appendBytes:"-" length:1];
  89. }
  90. [array appendBytes:string++ length:1];
  91. }
  92. }
  93. else
  94. {
  95. for( i=0; i < length; i++ )
  96. {
  97. if( i==0 )
  98. [array appendBytes:"(" length:1];
  99. if( i==3 )
  100. [array appendBytes:") " length:2];
  101. if( i==6 )
  102. [array appendBytes:"-" length:1];
  103. [array appendBytes:string++ length:1];
  104. }
  105. }
  106. [array appendBytes:"\0" length:1];
  107. return [NSString stringWithUTF8String:(const char*)[array bytes]];
  108. }
  109. }
  110. return phone;
  111. }
  112. +(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern
  113. {
  114. if(content==nil)
  115. return nil;
  116. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
  117. NSString* sss = [regex stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:to_pattern];
  118. return sss;
  119. }
  120. +(NSArray*) expression_varable:(NSString*)content regex:(NSString*) pattern
  121. {
  122. if(content==nil)
  123. return nil;
  124. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
  125. NSArray *matches = [regex matchesInString:content options:nil range:NSMakeRange(0, content.length)];
  126. if (matches) {
  127. for (NSTextCheckingResult *match in matches) {
  128. for (int i = 0; i < match.numberOfRanges; ++i) {
  129. DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
  130. }
  131. }
  132. }
  133. return matches;
  134. }
  135. +(NSTextCheckingResult*) expression_findfistMatch:(NSString*)content regex:(NSString*) pattern
  136. {
  137. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
  138. NSTextCheckingResult *match = [regex firstMatchInString:content options:nil range:NSMakeRange(0, content.length)];
  139. return match;
  140. // if (matches) {
  141. // for (NSTextCheckingResult *match in matches) {
  142. // for (int i = 0; i < match.numberOfRanges; ++i) {
  143. // DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
  144. // }
  145. // }
  146. // }
  147. // return matches;
  148. }
  149. + (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign
  150. {
  151. // double cx=parent.origin.x+parent.size.width/2;
  152. // double cy=parent.origin.y+parent.size.height/2;
  153. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  154. if([vAlign.lowercaseString isEqualToString:@"middle"])
  155. {
  156. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  157. }
  158. return rect;
  159. }
  160. @end