《省钱达人》与《猎豆优选》UI相同版。域名tbk

MLLinkLabel.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //
  2. // MLLinkLabel.m
  3. // MLLabel
  4. //
  5. // Created by molon on 15/6/6.
  6. // Copyright (c) 2015年 molon. All rights reserved.
  7. //
  8. #import "MLLinkLabel.h"
  9. #import "MLLabel+Override.h"
  10. #import "NSMutableAttributedString+MLLabel.h"
  11. #import "MLLabelLayoutManager.h"
  12. #define REGULAREXPRESSION_OPTION(regularExpression,regex,option) \
  13. \
  14. static NSRegularExpression * k##regularExpression() { \
  15. static NSRegularExpression *_##regularExpression = nil; \
  16. static dispatch_once_t onceToken; \
  17. dispatch_once(&onceToken, ^{ \
  18. _##regularExpression = [[NSRegularExpression alloc] initWithPattern:(regex) options:(option) error:nil];\
  19. });\
  20. \
  21. return _##regularExpression;\
  22. }\
  23. #define REGULAREXPRESSION(regularExpression,regex) REGULAREXPRESSION_OPTION(regularExpression,regex,NSRegularExpressionCaseInsensitive)
  24. REGULAREXPRESSION(URLRegularExpression,@"((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\\.\\-~!@#$%^&*+?:_/=<>]*)?)")
  25. REGULAREXPRESSION(PhoneNumerRegularExpression, @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}")
  26. REGULAREXPRESSION(EmailRegularExpression, @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}")
  27. REGULAREXPRESSION(UserHandleRegularExpression, @"@[\\u4e00-\\u9fa5\\w\\-]+")
  28. REGULAREXPRESSION(HashtagRegularExpression, @"#([\\u4e00-\\u9fa5\\w\\-]+)")
  29. @interface MLLink()
  30. @property (nonatomic, assign) NSRange linkRange;
  31. @end
  32. @implementation MLLink
  33. + (instancetype)linkWithType:(MLLinkType)type value:(NSString*)value range:(NSRange)range
  34. {
  35. return [MLLink linkWithType:type value:value range:range linkTextAttributes:nil activeLinkTextAttributes:nil];
  36. }
  37. + (instancetype)linkWithType:(MLLinkType)type value:(NSString*)value range:(NSRange)range linkTextAttributes:(NSDictionary*)linkTextAttributes activeLinkTextAttributes:(NSDictionary*)activeLinkTextAttributes
  38. {
  39. MLLink *link = [MLLink new];
  40. link.linkType = type;
  41. link.linkValue = value;
  42. link.linkRange = range;
  43. link.linkTextAttributes = linkTextAttributes;
  44. link.activeLinkTextAttributes = activeLinkTextAttributes;
  45. return link;
  46. }
  47. @end
  48. @interface MLLinkLabel()<UIGestureRecognizerDelegate>
  49. @property (nonatomic, strong) NSMutableArray *links;
  50. @property (nonatomic, strong) MLLink *activeLink;
  51. @property (nonatomic, assign) BOOL dontReCreateLinks;
  52. @property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer;
  53. @end
  54. @implementation MLLinkLabel
  55. #pragma mark - getter
  56. - (NSMutableArray *)links
  57. {
  58. if (!_links) {
  59. _links = [NSMutableArray array];
  60. }
  61. return _links;
  62. }
  63. - (UILongPressGestureRecognizer *)longPressGestureRecognizer
  64. {
  65. if (!_longPressGestureRecognizer) {
  66. _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureDidFire:)];
  67. _longPressGestureRecognizer.delegate = self;
  68. }
  69. return _longPressGestureRecognizer;
  70. }
  71. #pragma mark - setter
  72. - (void)setActiveLink:(MLLink *)activeLink
  73. {
  74. BOOL isUnChanged = (!activeLink&&!_activeLink)||[activeLink isEqual:_activeLink];
  75. _activeLink = activeLink;
  76. if (isUnChanged) {
  77. return;
  78. }
  79. [self reSetText];
  80. [CATransaction flush];
  81. }
  82. - (void)setAllowLineBreakInsideLinks:(BOOL)allowLineBreakInsideLinks
  83. {
  84. if (allowLineBreakInsideLinks==_allowLineBreakInsideLinks) return;
  85. _allowLineBreakInsideLinks = allowLineBreakInsideLinks;
  86. [self reSetText];
  87. }
  88. - (void)setLinkTextAttributes:(NSDictionary *)linkTextAttributes
  89. {
  90. _linkTextAttributes = linkTextAttributes;
  91. [self reSetText];
  92. }
  93. - (void)setActiveLinkTextAttributes:(NSDictionary *)activeLinkTextAttributes
  94. {
  95. _activeLinkTextAttributes = activeLinkTextAttributes;
  96. [self reSetText];
  97. }
  98. - (void)setDataDetectorTypes:(MLDataDetectorTypes)dataDetectorTypes
  99. {
  100. _dataDetectorTypes = dataDetectorTypes;
  101. [super reSetText];
  102. }
  103. - (void)setDataDetectorTypesOfAttributedLinkValue:(MLDataDetectorTypes)dataDetectorTypesOfAttributedLinkValue
  104. {
  105. _dataDetectorTypesOfAttributedLinkValue = dataDetectorTypesOfAttributedLinkValue;
  106. [super reSetText];
  107. }
  108. #pragma mark - override
  109. - (void)reSetText
  110. {
  111. //标记不重新生成链接,因为修改label的样式,例如字体啊什么的,父类会调用reSetText方法。而这时候如果依然重新生成link的话,会引起addLink方式后添加的link丢失。
  112. //然后此类内部所有需要重新生成链接的都是调用[super reSetText],否则只是需要重绘的调用[self reSetText]
  113. self.dontReCreateLinks = YES;
  114. [super reSetText];
  115. self.dontReCreateLinks = NO;
  116. }
  117. - (void)commonInit
  118. {
  119. [super commonInit];
  120. self.exclusiveTouch = YES;
  121. self.userInteractionEnabled = YES;
  122. self.activeLinkToNilDelay = 0.3f;
  123. //默认除了话题和@都检测
  124. _dataDetectorTypes = MLDataDetectorTypeURL|MLDataDetectorTypePhoneNumber|MLDataDetectorTypeEmail|MLDataDetectorTypeAttributedLink;
  125. _dataDetectorTypesOfAttributedLinkValue = MLDataDetectorTypeNone;
  126. _allowLineBreakInsideLinks = YES;
  127. [self addGestureRecognizer:self.longPressGestureRecognizer];
  128. }
  129. - (void)setText:(NSString *)text
  130. {
  131. //先提取出来links
  132. if (!self.dontReCreateLinks) {
  133. self.links = [self linksWithString:text];
  134. _activeLink = nil; //这里不能走setter
  135. }
  136. [super setText:text];
  137. }
  138. - (void)setAttributedText:(NSAttributedString *)attributedText
  139. {
  140. //先提取出来links
  141. if (!self.dontReCreateLinks) {
  142. self.links = [self linksWithString:attributedText];
  143. _activeLink = nil; //这里不能走setter
  144. }
  145. [super setAttributedText:attributedText];
  146. }
  147. - (NSMutableAttributedString*)attributedTextForTextStorageFromLabelProperties
  148. {
  149. NSMutableAttributedString *attributedString = [super attributedTextForTextStorageFromLabelProperties];
  150. //默认的链接样式不是我们想要的,去除它
  151. [attributedString removeAttribute:NSLinkAttributeName range:NSMakeRange(0, attributedString.length)];
  152. //检测是否有链接,有的话就直接给设置链接样式
  153. for (MLLink *link in self.links) {
  154. NSDictionary *attributes = nil;
  155. if ([link isEqual:self.activeLink]) {
  156. attributes = link.activeLinkTextAttributes?link.activeLinkTextAttributes:self.activeLinkTextAttributes;
  157. if (!attributes) {
  158. attributes = @{NSForegroundColorAttributeName:kDefaultLinkColorForMLLinkLabel,NSBackgroundColorAttributeName:kDefaultActiveLinkBackgroundColorForMLLinkLabel};
  159. }
  160. }else{
  161. attributes = link.linkTextAttributes?link.linkTextAttributes:self.linkTextAttributes;
  162. if (!attributes) {
  163. attributes = @{NSForegroundColorAttributeName:kDefaultLinkColorForMLLinkLabel};
  164. }
  165. }
  166. // [attributedString removeAttributes:[attributes allKeys] range:link.linkRange];
  167. [attributedString addAttributes:attributes range:link.linkRange];
  168. }
  169. return attributedString;
  170. }
  171. #pragma mark - 正则匹配相关
  172. static NSArray * kAllRegexps() {
  173. static NSArray *_allRegexps = nil;
  174. static dispatch_once_t onceToken;
  175. dispatch_once(&onceToken, ^{
  176. _allRegexps = @[kURLRegularExpression(),kPhoneNumerRegularExpression(),kEmailRegularExpression(),kUserHandleRegularExpression(),kHashtagRegularExpression()];
  177. });
  178. return _allRegexps;
  179. }
  180. - (NSArray*)regexpsWithDataDetectorTypes:(MLDataDetectorTypes)dataDetectorTypes
  181. {
  182. MLDataDetectorTypes const allDataDetectorTypes[] = {MLDataDetectorTypeURL,MLDataDetectorTypePhoneNumber,MLDataDetectorTypeEmail,MLDataDetectorTypeUserHandle,MLDataDetectorTypeHashtag};
  183. NSArray *allRegexps = kAllRegexps();
  184. NSMutableArray *regexps = [NSMutableArray array];
  185. for (NSInteger i=0; i<allRegexps.count; i++) {
  186. if (dataDetectorTypes&(allDataDetectorTypes[i])) {
  187. [regexps addObject:allRegexps[i]];
  188. }
  189. }
  190. return regexps.count>0?regexps:nil;
  191. }
  192. //根据dataDetectorTypes和string获取其linkType
  193. - (MLLinkType)linkTypeOfString:(NSString*)string withDataDetectorTypes:(MLDataDetectorTypes)dataDetectorTypes
  194. {
  195. if (dataDetectorTypes == MLDataDetectorTypeNone) {
  196. return MLLinkTypeOther;
  197. }
  198. NSArray *allRegexps = kAllRegexps();
  199. NSArray *regexps = [self regexpsWithDataDetectorTypes:dataDetectorTypes];
  200. NSRange textRange = NSMakeRange(0, string.length);
  201. for (NSRegularExpression *regexp in regexps) {
  202. NSTextCheckingResult *result = [regexp firstMatchInString:string options:NSMatchingAnchored range:textRange];
  203. if (result&&NSEqualRanges(result.range, textRange)) {
  204. //这个type确定
  205. MLLinkType linkType = [allRegexps indexOfObject:regexp]+1;
  206. return linkType;
  207. }
  208. }
  209. return MLLinkTypeOther;
  210. }
  211. - (NSMutableArray*)linksWithString:(id)string
  212. {
  213. if (self.dataDetectorTypes == MLDataDetectorTypeNone||!string) {
  214. return nil;
  215. }
  216. NSString *plainText = [string isKindOfClass:[NSAttributedString class]]?((NSAttributedString*)string).string:string;
  217. if (plainText.length<=0) {
  218. return nil;
  219. }
  220. NSMutableArray *links = [NSMutableArray array];
  221. if ((self.dataDetectorTypes&MLDataDetectorTypeAttributedLink)&&[string isKindOfClass:[NSAttributedString class]]) {
  222. NSAttributedString *attributedString = ((NSAttributedString*)string);
  223. [attributedString enumerateAttribute:NSLinkAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
  224. if (value) {
  225. NSString *linkValue = nil;
  226. if ([value isKindOfClass:[NSURL class]]) {
  227. linkValue = [value absoluteString];
  228. }else if ([value isKindOfClass:[NSString class]]) {
  229. linkValue = value;
  230. }else if ([value isKindOfClass:[NSAttributedString class]]) {
  231. linkValue = [value string];
  232. }
  233. if (linkValue.length>0) {
  234. MLLink *link = [MLLink linkWithType:[self linkTypeOfString:linkValue withDataDetectorTypes:self.dataDetectorTypesOfAttributedLinkValue] value:linkValue range:range];
  235. if (self.beforeAddLinkBlock) {
  236. self.beforeAddLinkBlock(link);
  237. }
  238. [links addObject:link];
  239. }
  240. }
  241. }];
  242. }
  243. NSArray *allRegexps = kAllRegexps();
  244. NSArray *regexps = [self regexpsWithDataDetectorTypes:self.dataDetectorTypes];
  245. NSRange textRange = NSMakeRange(0, plainText.length);
  246. for (NSRegularExpression *regexp in regexps) {
  247. [regexp enumerateMatchesInString:plainText options:0 range:textRange usingBlock:^(NSTextCheckingResult *result, __unused NSMatchingFlags flags, __unused BOOL *stop) {
  248. //去重处理
  249. for (MLLink *link in links){
  250. if (NSMaxRange(NSIntersectionRange(link.linkRange, result.range))>0){
  251. return;
  252. }
  253. }
  254. //这个刚好和MLLinkType对应
  255. MLLinkType linkType = [allRegexps indexOfObject:regexp]+1;
  256. if (linkType!=MLLinkTypeNone) {
  257. MLLink *link = [MLLink linkWithType:linkType value:[plainText substringWithRange:result.range] range:result.range];
  258. if (self.beforeAddLinkBlock) {
  259. self.beforeAddLinkBlock(link);
  260. }
  261. [links addObject:link];
  262. }
  263. }];
  264. }
  265. return links.count>0?links:nil;
  266. }
  267. #pragma mark - 链接点击交互相关
  268. - (MLLink *)linkAtPoint:(CGPoint)location
  269. {
  270. if (self.links.count<=0||self.textStorage.string.length == 0||self.textContainer.size.width<=0||self.textContainer.size.height<=0)
  271. {
  272. return nil;
  273. }
  274. CGPoint textOffset;
  275. //在执行usedRectForTextContainer之前最好还是执行下glyphRangeForTextContainer relayout
  276. [self.layoutManager glyphRangeForTextContainer:self.textContainer];
  277. textOffset = [self textOffsetWithTextSize:[self.layoutManager usedRectForTextContainer:self.textContainer].size];
  278. //location转换成在textContainer的绘制区域的坐标
  279. location.x -= textOffset.x;
  280. location.y -= textOffset.y;
  281. //获取触摸的字形
  282. NSUInteger glyphIdx = [self.layoutManager glyphIndexForPoint:location inTextContainer:self.textContainer];
  283. //apple文档上写有说 如果location的区域没字形,可能返回的是最近的字形index,所以我们再找到这个字形所处于的rect来确认
  284. CGRect glyphRect = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIdx, 1)
  285. inTextContainer:self.textContainer];
  286. if (!CGRectContainsPoint(glyphRect, location))
  287. return nil;
  288. NSUInteger charIndex = [self.layoutManager characterIndexForGlyphAtIndex:glyphIdx];
  289. //找到了charIndex,然后去寻找是否这个字处于链接内部
  290. for (MLLink *link in self.links) {
  291. if (NSLocationInRange(charIndex,link.linkRange)) {
  292. return link;
  293. }
  294. }
  295. return nil;
  296. }
  297. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  298. {
  299. UITouch *touch = [touches anyObject];
  300. self.activeLink = [self linkAtPoint:[touch locationInView:self]];
  301. //如果已经触发了链接,就不朝上传递消息了
  302. if (!self.activeLink) {
  303. [super touchesBegan:touches withEvent:event];
  304. }
  305. }
  306. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  307. {
  308. //如果当前位置和之前的active的不一样的话,就认为不选那个链接了
  309. if (self.activeLink) {
  310. UITouch *touch = [touches anyObject];
  311. if (![self.activeLink isEqual:[self linkAtPoint:[touch locationInView:self]]]) {
  312. self.activeLink = nil;
  313. }
  314. } else {
  315. [super touchesMoved:touches withEvent:event];
  316. }
  317. }
  318. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  319. {
  320. if (self.activeLink) {
  321. NSString *linkText = [self.textStorage.string substringWithRange:self.activeLink.linkRange];
  322. //告诉外面已经点击了某链接
  323. if (self.activeLink.didClickLinkBlock) {
  324. self.activeLink.didClickLinkBlock(self.activeLink,linkText,self);
  325. }else if (self.didClickLinkBlock) {
  326. self.didClickLinkBlock(self.activeLink,linkText,self);
  327. }else if(self.delegate&&[self.delegate respondsToSelector:@selector(didClickLink:linkText:linkLabel:)]){
  328. [self.delegate didClickLink:self.activeLink linkText:linkText linkLabel:self];
  329. }
  330. [self performSelector:@selector(setActiveLink:) withObject:nil afterDelay:self.activeLinkToNilDelay];
  331. } else {
  332. [super touchesEnded:touches withEvent:event];
  333. }
  334. }
  335. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  336. {
  337. if (self.activeLink) {
  338. self.activeLink = nil;
  339. } else {
  340. [super touchesCancelled:touches withEvent:event];
  341. }
  342. }
  343. #pragma mark - 长按相关
  344. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  345. MLLink *link = [self linkAtPoint:[touch locationInView:self]];
  346. if (link) {
  347. //检测是否有长按回调,没的话就不继续
  348. if ((self.delegate&&[self.delegate respondsToSelector:@selector(didLongPressLink:linkText:linkLabel:)])
  349. ||self.didLongPressLinkBlock
  350. ||link.didLongPressLinkBlock) {
  351. return YES;
  352. }
  353. }
  354. return NO;
  355. }
  356. - (void)longPressGestureDidFire:(UILongPressGestureRecognizer *)sender {
  357. if (sender.state==UIGestureRecognizerStateBegan) {
  358. MLLink *link = [self linkAtPoint:[sender locationInView:self]];
  359. if (link) {
  360. NSString *linkText = [self.textStorage.string substringWithRange:link.linkRange];
  361. //告诉外面已经长按了某链接
  362. if (link.didLongPressLinkBlock) {
  363. link.didLongPressLinkBlock(link,linkText,self);
  364. }else if (self.didLongPressLinkBlock) {
  365. self.didLongPressLinkBlock(link,linkText,self);
  366. }else if (self.delegate&&[self.delegate respondsToSelector:@selector(didLongPressLink:linkText:linkLabel:)]){
  367. [self.delegate didLongPressLink:link linkText:linkText linkLabel:self];
  368. }
  369. }
  370. }
  371. }
  372. #pragma mark - 外部调用相关
  373. - (BOOL)addLink:(MLLink*)link
  374. {
  375. return [self addLinks:@[link]].count>0;
  376. }
  377. - (MLLink*)addLinkWithType:(MLLinkType)type value:(NSString*)value range:(NSRange)range
  378. {
  379. MLLink *link = [MLLink linkWithType:type value:value range:range];
  380. return [self addLink:link]?link:nil;
  381. }
  382. - (NSArray*)addLinks:(NSArray*)links
  383. {
  384. NSMutableArray *validLinks = [NSMutableArray arrayWithCapacity:links.count];
  385. for (MLLink *link in links) {
  386. if (!link||NSMaxRange(link.linkRange)>self.textStorage.length) {
  387. continue;
  388. }
  389. //检测是否此位置已经有东西占用
  390. for (MLLink *aLink in self.links){
  391. if (NSMaxRange(NSIntersectionRange(aLink.linkRange, link.linkRange))>0){
  392. continue;
  393. }
  394. }
  395. if (self.beforeAddLinkBlock) {
  396. self.beforeAddLinkBlock(link);
  397. }
  398. //加入它
  399. [self.links addObject:link];
  400. [validLinks addObject:link];
  401. }
  402. //重绘
  403. [self reSetText];
  404. return validLinks;
  405. }
  406. - (void)invalidateDisplayForLinks
  407. {
  408. [self reSetText];
  409. }
  410. #pragma mark - 布局相关
  411. -(BOOL)layoutManager:(NSLayoutManager *)layoutManager shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex
  412. {
  413. if (self.lineBreakMode == NSLineBreakByCharWrapping) {
  414. return NO;
  415. }
  416. if (self.allowLineBreakInsideLinks) {
  417. return YES;
  418. }
  419. //让在链接区间下,尽量不break
  420. for (MLLink *link in self.links) {
  421. if (NSLocationInRange(charIndex,link.linkRange)) {
  422. return NO;
  423. }
  424. }
  425. return YES;
  426. }
  427. @end