AttributedLabel.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // AttributedLabel.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain.lee on 2018/5/15.
  6. // Copyright © 2018 WildFireChat. All rights reserved.
  7. //
  8. #import "AttributedLabel.h"
  9. #import <CoreText/CoreText.h>
  10. @interface AttributedLabel()
  11. @property(nonatomic, strong)NSMutableArray *stringArray;
  12. @property(nonatomic, strong)NSMutableArray *rangeArray;
  13. @end
  14. @implementation AttributedLabel
  15. - (void)setText:(NSString *)text {
  16. self.attributedText = [self subStr:text];
  17. self.userInteractionEnabled = YES;
  18. }
  19. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  20. UITouch *touch = [touches anyObject];
  21. CFIndex index = [self characterIndexAtPoint:[touch locationInView:self]];
  22. for(NSValue *value in self.rangeArray) {
  23. NSRange range=[value rangeValue];
  24. if (range.location <= index && (range.location+range.length) >= index) {
  25. NSInteger i=[self.rangeArray indexOfObject:value];
  26. NSString *str = self.stringArray[i];
  27. NSLog(@"touch url %@", str);
  28. NSString *pattern =@"[0-9]{5,12}";
  29. NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
  30. BOOL isNumber = [pred evaluateWithObject:str];
  31. if (isNumber) {
  32. if ([self.attributedLabelDelegate respondsToSelector:@selector(didSelectPhoneNumber:)]) {
  33. [self.attributedLabelDelegate didSelectPhoneNumber:str];
  34. }
  35. } else {
  36. if ([self.attributedLabelDelegate respondsToSelector:@selector(didSelectUrl:)]) {
  37. [self.attributedLabelDelegate didSelectUrl:str];
  38. }
  39. }
  40. }
  41. }
  42. [super touchesBegan:touches withEvent:event];
  43. }
  44. -(NSMutableAttributedString*)subStr:(NSString *)string {
  45. if (!string) {
  46. return nil;
  47. }
  48. NSError *error;
  49. //可以识别url的正则表达式
  50. NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
  51. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
  52. options:NSRegularExpressionCaseInsensitive
  53. error:&error];
  54. NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
  55. NSMutableArray *arr=[[NSMutableArray alloc]init];
  56. NSMutableArray *rangeArr=[[NSMutableArray alloc]init];
  57. self.stringArray = arr;
  58. self.rangeArray = rangeArr;
  59. for (NSTextCheckingResult *match in arrayOfAllMatches) {
  60. NSString* substringForMatch;
  61. substringForMatch = [string substringWithRange:match.range];
  62. [arr addObject:substringForMatch];
  63. }
  64. NSString *subStr=string;
  65. for (NSString *str in arr) {
  66. [rangeArr addObject:[self rangesOfString:str inString:subStr]];
  67. }
  68. NSString *pattern =@"[0-9]{5,11}";
  69. regex = [NSRegularExpression regularExpressionWithPattern:pattern
  70. options:NSRegularExpressionCaseInsensitive
  71. error:&error];
  72. arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
  73. NSMutableArray *telArr = [[NSMutableArray alloc] init];
  74. for (NSTextCheckingResult *match in arrayOfAllMatches) {
  75. NSString* substringForMatch;
  76. substringForMatch = [string substringWithRange:match.range];
  77. [arr addObject:substringForMatch];
  78. [telArr addObject:substringForMatch];
  79. }
  80. subStr=string;
  81. for (NSString *str in telArr) {
  82. [rangeArr addObject:[self rangesOfString:str inString:subStr]];
  83. }
  84. NSMutableAttributedString *attributedText;
  85. attributedText=[[NSMutableAttributedString alloc]initWithString:subStr attributes:@{NSFontAttributeName :self.font}];
  86. for(NSValue *value in rangeArr) {
  87. NSInteger index=[rangeArr indexOfObject:value];
  88. NSRange range=[value rangeValue];
  89. [attributedText addAttribute:NSLinkAttributeName value:[NSURL URLWithString:[arr objectAtIndex:index]] range:range];
  90. [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
  91. }
  92. return attributedText;
  93. }
  94. //获取查找字符串在母串中的NSRange
  95. - (NSValue *)rangesOfString:(NSString *)searchString inString:(NSString *)str {
  96. NSRange searchRange = NSMakeRange(0, [str length]);
  97. NSRange range;
  98. if ((range = [str rangeOfString:searchString options:0 range:searchRange]).location != NSNotFound) {
  99. searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
  100. }
  101. return [NSValue valueWithRange:range];
  102. }
  103. - (CFIndex)characterIndexAtPoint:(CGPoint)point {
  104. ////////
  105. NSMutableAttributedString* optimizedAttributedText = [self.attributedText mutableCopy];
  106. [self.attributedText enumerateAttribute:(NSString*)kCTParagraphStyleAttributeName inRange:NSMakeRange(0, [optimizedAttributedText length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
  107. if (value == nil) {
  108. return ;
  109. }
  110. NSMutableParagraphStyle* paragraphStyle = [value mutableCopy];
  111. if ([paragraphStyle lineBreakMode] == kCTLineBreakByTruncatingTail) {
  112. [paragraphStyle setLineBreakMode:kCTLineBreakByWordWrapping];
  113. }
  114. [optimizedAttributedText removeAttribute:(NSString*)kCTParagraphStyleAttributeName range:range];
  115. [optimizedAttributedText addAttribute:(NSString*)kCTParagraphStyleAttributeName value:paragraphStyle range:range];
  116. }];
  117. ////////
  118. if (!CGRectContainsPoint(self.bounds, point)) {
  119. return NSNotFound;
  120. }
  121. CGRect textRect = [self textRect];
  122. if (!CGRectContainsPoint(textRect, point)) {
  123. return NSNotFound;
  124. }
  125. // Offset tap coordinates by textRect origin to make them relative to the origin of frame
  126. point = CGPointMake(point.x - textRect.origin.x, point.y - textRect.origin.y);
  127. // Convert tap coordinates (start at top left) to CT coordinates (start at bottom left)
  128. point = CGPointMake(point.x, textRect.size.height - point.y);
  129. //////
  130. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)optimizedAttributedText);
  131. CGMutablePathRef path = CGPathCreateMutable();
  132. CGPathAddRect(path, NULL, textRect);
  133. CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [self.attributedText length]), path, NULL);
  134. if (frame == NULL) {
  135. CFRelease(path);
  136. return NSNotFound;
  137. }
  138. CFArrayRef lines = CTFrameGetLines(frame);
  139. NSInteger numberOfLines = self.numberOfLines > 0 ? MIN(self.numberOfLines, CFArrayGetCount(lines)) : CFArrayGetCount(lines);
  140. //NSLog(@"num lines: %d", numberOfLines);
  141. if (numberOfLines == 0) {
  142. CFRelease(frame);
  143. CFRelease(path);
  144. return NSNotFound;
  145. }
  146. NSUInteger idx = NSNotFound;
  147. CGPoint lineOrigins[numberOfLines];
  148. CTFrameGetLineOrigins(frame, CFRangeMake(0, numberOfLines), lineOrigins);
  149. for (CFIndex lineIndex = 0; lineIndex < numberOfLines; lineIndex++) {
  150. CGPoint lineOrigin = lineOrigins[lineIndex];
  151. CTLineRef line = CFArrayGetValueAtIndex(lines, lineIndex);
  152. // Get bounding information of line
  153. CGFloat ascent, descent, leading, width;
  154. width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
  155. CGFloat yMin = floor(lineOrigin.y - descent);
  156. CGFloat yMax = ceil(lineOrigin.y + ascent);
  157. // Check if we've already passed the line
  158. if (point.y > yMax) {
  159. break;
  160. }
  161. // Check if the point is within this line vertically
  162. if (point.y >= yMin) {
  163. // Check if the point is within this line horizontally
  164. if (point.x >= lineOrigin.x && point.x <= lineOrigin.x + width) {
  165. // Convert CT coordinates to line-relative coordinates
  166. CGPoint relativePoint = CGPointMake(point.x - lineOrigin.x, point.y - lineOrigin.y);
  167. idx = CTLineGetStringIndexForPosition(line, relativePoint);
  168. break;
  169. }
  170. }
  171. }
  172. CFRelease(frame);
  173. CFRelease(path);
  174. return idx;
  175. }
  176. - (CGRect)textRect {
  177. CGRect textRect = [self textRectForBounds:self.bounds limitedToNumberOfLines:self.numberOfLines];
  178. textRect.origin.y = (self.bounds.size.height - textRect.size.height)/2;
  179. if (self.textAlignment == NSTextAlignmentCenter) {
  180. textRect.origin.x = (self.bounds.size.width - textRect.size.width)/2;
  181. }
  182. if (self.textAlignment == NSTextAlignmentRight) {
  183. textRect.origin.x = self.bounds.size.width - textRect.size.width;
  184. }
  185. return textRect;
  186. }
  187. @end