Skip to content

Commit df9e222

Browse files
committed
Add NSLink Support for tap Action
1 parent 7bd2aa4 commit df9e222

File tree

8 files changed

+86
-19
lines changed

8 files changed

+86
-19
lines changed

Demo/YYTextDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
010A65AF261F028000753A75 /* YYTextHighlight+NSLink.m in Sources */ = {isa = PBXBuildFile; fileRef = 010A65AE261F028000753A75 /* YYTextHighlight+NSLink.m */; };
1011
D91054861F3735D20007F224 /* Animated image support.txt in Resources */ = {isa = PBXBuildFile; fileRef = D910547B1F3735D20007F224 /* Animated image support.txt */; };
1112
D91054871F3735D20007F224 /* YYAnimatedImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = D910547D1F3735D20007F224 /* YYAnimatedImageView.m */; };
1213
D91054881F3735D20007F224 /* YYFrameImage.m in Sources */ = {isa = PBXBuildFile; fileRef = D910547F1F3735D20007F224 /* YYFrameImage.m */; };
@@ -38,7 +39,6 @@
3839
D91054D71F3735E50007F224 /* YYLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054BD1F3735E50007F224 /* YYLabel.m */; };
3940
D91054D81F3735E50007F224 /* YYTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054C01F3735E50007F224 /* YYTextView.m */; };
4041
D91054DD1F3736060007F224 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054DA1F3736060007F224 /* AppDelegate.m */; };
41-
D91054DE1F3736060007F224 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054DC1F3736060007F224 /* ViewController.m */; };
4242
D91054F61F3736700007F224 /* CALayer+YYAdd.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054E11F3736700007F224 /* CALayer+YYAdd.m */; };
4343
D91054F71F3736700007F224 /* NSBundle+YYAdd.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054E31F3736700007F224 /* NSBundle+YYAdd.m */; };
4444
D91054F81F3736700007F224 /* NSData+YYAdd.m in Sources */ = {isa = PBXBuildFile; fileRef = D91054E51F3736700007F224 /* NSData+YYAdd.m */; };
@@ -82,6 +82,8 @@
8282
/* End PBXBuildFile section */
8383

8484
/* Begin PBXFileReference section */
85+
010A65AD261F028000753A75 /* YYTextHighlight+NSLink.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "YYTextHighlight+NSLink.h"; sourceTree = "<group>"; };
86+
010A65AE261F028000753A75 /* YYTextHighlight+NSLink.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "YYTextHighlight+NSLink.m"; sourceTree = "<group>"; };
8587
D910547B1F3735D20007F224 /* Animated image support.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Animated image support.txt"; sourceTree = "<group>"; };
8688
D910547C1F3735D20007F224 /* YYAnimatedImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YYAnimatedImageView.h; sourceTree = "<group>"; };
8789
D910547D1F3735D20007F224 /* YYAnimatedImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYAnimatedImageView.m; sourceTree = "<group>"; };
@@ -144,8 +146,6 @@
144146
D91054C01F3735E50007F224 /* YYTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYTextView.m; sourceTree = "<group>"; };
145147
D91054D91F3736060007F224 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
146148
D91054DA1F3736060007F224 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
147-
D91054DB1F3736060007F224 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
148-
D91054DC1F3736060007F224 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
149149
D91054E01F3736700007F224 /* CALayer+YYAdd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CALayer+YYAdd.h"; sourceTree = "<group>"; };
150150
D91054E11F3736700007F224 /* CALayer+YYAdd.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CALayer+YYAdd.m"; sourceTree = "<group>"; };
151151
D91054E21F3736700007F224 /* NSBundle+YYAdd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle+YYAdd.h"; sourceTree = "<group>"; };
@@ -306,6 +306,8 @@
306306
D91054A81F3735E50007F224 /* YYTextRubyAnnotation.m */,
307307
D91054A91F3735E50007F224 /* YYTextRunDelegate.h */,
308308
D91054AA1F3735E50007F224 /* YYTextRunDelegate.m */,
309+
010A65AD261F028000753A75 /* YYTextHighlight+NSLink.h */,
310+
010A65AE261F028000753A75 /* YYTextHighlight+NSLink.m */,
309311
);
310312
path = String;
311313
sourceTree = "<group>";
@@ -360,8 +362,6 @@
360362
D91054F51F3736700007F224 /* YYWeakProxy.m */,
361363
D91054D91F3736060007F224 /* AppDelegate.h */,
362364
D91054DA1F3736060007F224 /* AppDelegate.m */,
363-
D91054DB1F3736060007F224 /* ViewController.h */,
364-
D91054DC1F3736060007F224 /* ViewController.m */,
365365
D94EE9941F37304200F37AD6 /* Main.storyboard */,
366366
D94EE9971F37304200F37AD6 /* Assets.xcassets */,
367367
D94EE9991F37304200F37AD6 /* LaunchScreen.storyboard */,
@@ -538,6 +538,7 @@
538538
D91054D11F3735E50007F224 /* UIPasteboard+YYText.m in Sources */,
539539
D91054F61F3736700007F224 /* CALayer+YYAdd.m in Sources */,
540540
D91054D01F3735E50007F224 /* NSParagraphStyle+YYText.m in Sources */,
541+
010A65AF261F028000753A75 /* YYTextHighlight+NSLink.m in Sources */,
541542
D91054FD1F3736700007F224 /* UIView+YYAdd.m in Sources */,
542543
D91054D51F3735E50007F224 /* YYTextUtilities.m in Sources */,
543544
D91054D71F3735E50007F224 /* YYLabel.m in Sources */,
@@ -546,7 +547,6 @@
546547
D91054F81F3736700007F224 /* NSData+YYAdd.m in Sources */,
547548
D91054F71F3736700007F224 /* NSBundle+YYAdd.m in Sources */,
548549
D91054C81F3735E50007F224 /* YYTextMagnifier.m in Sources */,
549-
D91054DE1F3736060007F224 /* ViewController.m in Sources */,
550550
D910551D1F37367B0007F224 /* YYTextAttributeExample.m in Sources */,
551551
D91055261F37367B0007F224 /* YYTextTagExample.m in Sources */,
552552
D91054C21F3735E50007F224 /* YYTextDebugOption.m in Sources */,

Framework/YYText.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
010A65B4261F04C900753A75 /* YYTextHighlight+NSLink.h in Headers */ = {isa = PBXBuildFile; fileRef = 010A65B2261F04C900753A75 /* YYTextHighlight+NSLink.h */; settings = {ATTRIBUTES = (Public, ); }; };
11+
010A65B5261F04C900753A75 /* YYTextHighlight+NSLink.m in Sources */ = {isa = PBXBuildFile; fileRef = 010A65B3261F04C900753A75 /* YYTextHighlight+NSLink.m */; };
1012
D995E6BA1F372EC800EBEE44 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D918BFE71F372EA000106E14 /* MobileCoreServices.framework */; };
1113
D995E6BB1F372EC800EBEE44 /* Accelerate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D918BFE61F372E9B00106E14 /* Accelerate.framework */; };
1214
D995E6BC1F372EC800EBEE44 /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D918BFE51F372E9800106E14 /* CoreText.framework */; };
@@ -65,6 +67,8 @@
6567
/* End PBXBuildFile section */
6668

6769
/* Begin PBXFileReference section */
70+
010A65B2261F04C900753A75 /* YYTextHighlight+NSLink.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "YYTextHighlight+NSLink.h"; sourceTree = "<group>"; };
71+
010A65B3261F04C900753A75 /* YYTextHighlight+NSLink.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "YYTextHighlight+NSLink.m"; sourceTree = "<group>"; };
6872
D918BFD61F372E3000106E14 /* YYText.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = YYText.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6973
D918BFDA1F372E3000106E14 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7074
D918BFE21F372E6800106E14 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
@@ -226,6 +230,8 @@
226230
D995E6DC1F372ED100EBEE44 /* YYTextRubyAnnotation.m */,
227231
D995E6DD1F372ED100EBEE44 /* YYTextRunDelegate.h */,
228232
D995E6DE1F372ED100EBEE44 /* YYTextRunDelegate.m */,
233+
010A65B2261F04C900753A75 /* YYTextHighlight+NSLink.h */,
234+
010A65B3261F04C900753A75 /* YYTextHighlight+NSLink.m */,
229235
);
230236
path = String;
231237
sourceTree = "<group>";
@@ -284,6 +290,7 @@
284290
D995E7011F372ED100EBEE44 /* YYTextLine.h in Headers */,
285291
D995E6F51F372ED100EBEE44 /* YYTextContainerView.h in Headers */,
286292
D995E6FF1F372ED100EBEE44 /* YYTextLayout.h in Headers */,
293+
010A65B4261F04C900753A75 /* YYTextHighlight+NSLink.h in Headers */,
287294
D995E7071F372ED100EBEE44 /* YYTextArchiver.h in Headers */,
288295
);
289296
runOnlyForDeploymentPostprocessing = 0;
@@ -370,6 +377,7 @@
370377
D995E7081F372ED100EBEE44 /* YYTextArchiver.m in Sources */,
371378
D995E7101F372ED100EBEE44 /* YYTextRunDelegate.m in Sources */,
372379
D995E6F81F372ED100EBEE44 /* YYTextDebugOption.m in Sources */,
380+
010A65B5261F04C900753A75 /* YYTextHighlight+NSLink.m in Sources */,
373381
D995E6FA1F372ED100EBEE44 /* YYTextEffectWindow.m in Sources */,
374382
D995E7041F372ED100EBEE44 /* YYTextMagnifier.m in Sources */,
375383
D995E7181F372ED100EBEE44 /* UIView+YYText.m in Sources */,

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ Powerful text framework for iOS to display and edit rich text.<br/>
1313

1414
Features
1515
==============
16+
### New
17+
- Add support for `NSLink`
18+
- Add `AutoLayout` support for YYTextView
1619

20+
### Others
1721
- UILabel and UITextView API compatible
1822
- High performance asynchronous text layout and rendering
1923
- Extended CoreText attributes with more text effects
@@ -253,7 +257,7 @@ textView.dataDetectorTypes = ...
253257
textView.placeHolderText = ...
254258
textView.placeHolderTextColor = ...
255259
textView.delegate = ...
256-
```
260+
```
257261

258262
### Attributed text
259263
```objc
@@ -277,7 +281,7 @@ textView.attributedString = text;
277281
```
278282
279283
### Text highlight
280-
284+
281285
You can use some convenience methods to set text highlight:
282286
```objc
283287
[text yy_setTextHighlightRange:range
@@ -399,8 +403,8 @@ YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
399403
YYLabel *label = [YYLabel new];
400404
label.size = layout.textBoundingSize;
401405
label.textLayout = layout;
402-
```
403-
406+
```
407+
404408
### Asynchronous layout and rendering
405409
```objc
406410
// If you have performance issues,
@@ -449,7 +453,7 @@ textView.exclusionPaths = @[[UIBezierPath bezierPathWith...];,...];
449453
textView.textContainerInset = UIEdgeInsetsMake(...);
450454
textView.verticalForm = YES/NO;
451455
```
452-
456+
453457
### Text parser
454458
```objc
455459
// 1. Create a text parser
@@ -474,7 +478,7 @@ label.textParser = parser;
474478
YYTextView *textView = ...
475479
textView.textParser = parser;
476480
```
477-
481+
478482
### Debug
479483
```objc
480484
// Set a shared debug option to show text layout result.
@@ -796,7 +800,7 @@ textView.dataDetectorTypes = ...
796800
textView.placeHolderText = ...
797801
textView.placeHolderTextColor = ...
798802
textView.delegate = ...
799-
```
803+
```
800804

801805
### 属性文本
802806
```objc
@@ -817,7 +821,7 @@ label.attributedString = text;
817821
YYTextView *textView = [YYTextView new];
818822
textView.frame = ...
819823
textView.attributedString = text;
820-
```
824+
```
821825
822826
### 文本高亮
823827
@@ -917,7 +921,7 @@ YYLabel *label = [YYLabel new];
917921
label.size = layout.textBoundingSize;
918922
label.textLayout = layout;
919923
```
920-
924+
921925
### 文本行位置调整
922926
```objc
923927
// 由于中文、英文、Emoji 等字体高度不一致,或者富文本中出现了不同字号的字体,
@@ -992,7 +996,7 @@ YYTextView *textView = ...
992996
textView.exclusionPaths = @[[UIBezierPath bezierPathWith...];,...];
993997
textView.textContainerInset = UIEdgeInsetsMake(...);
994998
textView.verticalForm = YES/NO;
995-
```
999+
```
9961000

9971001
### 文本解析
9981002
```objc
@@ -1090,7 +1094,7 @@ debugOptions.CGGlyphBorderColor = [UIColor colorWithRed:1.000 green:0.524 blue:0
10901094

10911095
已知问题
10921096
==============
1093-
* YYText 并不能支持所有 CoreText/TextKit 的属性,比如 NSBackgroundColor、NSStrikethrough、NSUnderline、NSAttachment、NSLink 等,但 YYText 中基本都有对应属性作为替代。详情见上方表格。
1097+
* YYText 并不能支持所有 CoreText/TextKit 的属性,比如 NSBackgroundColor、NSStrikethrough、NSUnderline、NSAttachment、~~NSLink~~ 等,但 YYText 中基本都有对应属性作为替代。详情见上方表格。
10941098
* YYTextView 未实现局部刷新,所以在输入和编辑大量的文本(比如超过大概五千个汉字、或大概一万个英文字符)时会出现较明显的卡顿现象。
10951099
* 竖排版时,添加 exclusionPaths 在少数情况下可能会导致文本显示空白。
10961100
* 当添加了非矩形的 textContainerPath,并且有嵌入大于文本排版方向宽度的 RunDelegate 时,RunDelegate 之后的文字会无法显示。这是 CoreText 的 Bug(或者说是 Feature)。

YYText/Component/YYTextLayout.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ + (YYTextLayout *)layoutWithContainer:(YYTextContainer *)container text:(NSAttri
821821
layout.needDrawText = YES;
822822

823823
void (^block)(NSDictionary *attrs, NSRange range, BOOL *stop) = ^(NSDictionary *attrs, NSRange range, BOOL *stop) {
824-
if (attrs[YYTextHighlightAttributeName]) layout.containsHighlight = YES;
824+
if (attrs[YYTextHighlightAttributeName] || attrs[NSLinkAttributeName]) layout.containsHighlight = YES;
825825
if (attrs[YYTextBlockBorderAttributeName]) layout.needDrawBlockBorder = YES;
826826
if (attrs[YYTextBackgroundBorderAttributeName]) layout.needDrawBackgroundBorder = YES;
827827
if (attrs[YYTextShadowAttributeName] || attrs[NSShadowAttributeName]) layout.needDrawShadow = YES;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// YYTextHighlight+NSLink.h
3+
// YYTextDemo
4+
//
5+
// Created by Frank on 2021/4/8.
6+
// Copyright © 2021 ibireme. All rights reserved.
7+
//
8+
9+
#import "YYTextAttribute.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface YYTextHighlight (NSLink)
14+
15+
@property (nonatomic) NSURL *link;
16+
17+
@end
18+
19+
NS_ASSUME_NONNULL_END
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// YYTextHighlight+NSLink.m
3+
// YYTextDemo
4+
//
5+
// Created by Frank on 2021/4/8.
6+
// Copyright © 2021 ibireme. All rights reserved.
7+
//
8+
9+
#import "YYTextHighlight+NSLink.h"
10+
11+
@implementation YYTextHighlight (NSLink)
12+
13+
- (NSURL *)link {
14+
return self.userInfo[@"NSLink.URL"];
15+
}
16+
17+
- (void)setLink:(NSURL *)link {
18+
NSMutableDictionary *info = self.userInfo ? [self.userInfo mutableCopy] : [NSMutableDictionary new];
19+
info[@"NSLink.URL"] = link;
20+
self.userInfo = info;
21+
}
22+
23+
@end

YYText/YYText.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ FOUNDATION_EXPORT const unsigned char YYTextVersionString[];
3030
#import <YYText/NSAttributedString+YYText.h>
3131
#import <YYText/NSParagraphStyle+YYText.h>
3232
#import <YYText/UIPasteboard+YYText.h>
33+
#import <YYText/YYTextHighlight+NSLink.h>
3334
#else
3435
#import "YYLabel.h"
3536
#import "YYTextView.h"
@@ -47,4 +48,5 @@ FOUNDATION_EXPORT const unsigned char YYTextVersionString[];
4748
#import "NSAttributedString+YYText.h"
4849
#import "NSParagraphStyle+YYText.h"
4950
#import "UIPasteboard+YYText.h"
51+
#import "YYTextHighlight+NSLink.h"
5052
#endif

YYText/YYTextView.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "NSAttributedString+YYText.h"
2323
#import "UIPasteboard+YYText.h"
2424
#import "UIView+YYText.h"
25+
#import "YYTextHighlight+NSLink.h"
2526

2627

2728
static double _YYDeviceSystemVersion() {
@@ -1150,7 +1151,17 @@ - (YYTextHighlight *)_getHighlightAtPoint:(CGPoint)point range:(NSRangePointer)r
11501151
longestEffectiveRange:&highlightRange
11511152
inRange:NSMakeRange(0, _innerText.length)];
11521153

1153-
if (!highlight) return nil;
1154+
if (!highlight) {
1155+
// No highlight. So we check NSLink and create a highlight for NSLink
1156+
NSURL *link = [text attribute:NSLinkAttributeName
1157+
atIndex:startIndex
1158+
longestEffectiveRange:&highlightRange
1159+
inRange:NSMakeRange(0, _innerText.length)];
1160+
if (!link) return nil;
1161+
1162+
highlight = [YYTextHighlight new];
1163+
highlight.link = link;
1164+
}
11541165

11551166
BOOL shouldTap = YES, shouldLongPress = YES;
11561167
if (!highlight.tapAction && !highlight.longPressAction) {

0 commit comments

Comments
 (0)