Skip to content

Commit decf0bb

Browse files
author
amao
committed
调整 link detector 和 max sync length
1 parent cffc287 commit decf0bb

5 files changed

Lines changed: 37 additions & 40 deletions

File tree

M80AttributedLabel/M80AttributedLabel.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ NS_ASSUME_NONNULL_BEGIN
1414
@class M80AttributedLabelURL;
1515

1616
@interface M80AttributedLabel : UIView
17-
@property (nonatomic,weak,nullable) id<M80AttributedLabelDelegate> delegate;
18-
@property (nonatomic,strong,nullable) UIFont *font; //字体
19-
@property (nonatomic,strong,nullable) UIColor *textColor; //文字颜色
20-
@property (nonatomic,strong,nullable) UIColor *highlightColor; //链接点击时背景高亮色
21-
@property (nonatomic,strong,nullable) UIColor *linkColor; //链接色
22-
@property (nonatomic,strong,nullable) UIColor *shadowColor; //阴影颜色
23-
@property (nonatomic,assign) CGSize shadowOffset; //阴影offset
24-
@property (nonatomic,assign) CGFloat shadowBlur; //阴影半径
25-
@property (nonatomic,assign) BOOL underLineForLink; //链接是否带下划线
26-
@property (nonatomic,assign) BOOL autoDetectLinks; //自动检测
27-
@property (nonatomic,assign) NSInteger numberOfLines; //行数
28-
@property (nonatomic,assign) CTTextAlignment textAlignment; //文字排版样式
29-
@property (nonatomic,assign) CTLineBreakMode lineBreakMode; //LineBreakMode
30-
@property (nonatomic,assign) CGFloat lineSpacing; //行间距
31-
@property (nonatomic,assign) CGFloat paragraphSpacing; //段间距
32-
@property (nonatomic,copy,nullable) NSString *text; //普通文本
33-
@property (nonatomic,copy,nullable) NSAttributedString *attributedText; //属性文本
17+
@property (nonatomic,weak,nullable) id<M80AttributedLabelDelegate> delegate;
18+
@property (nonatomic,strong,nullable) UIFont *font; //字体
19+
@property (nonatomic,strong,nullable) UIColor *textColor; //文字颜色
20+
@property (nonatomic,strong,nullable) UIColor *highlightColor; //链接点击时背景高亮色
21+
@property (nonatomic,strong,nullable) UIColor *linkColor; //链接色
22+
@property (nonatomic,strong,nullable) UIColor *shadowColor; //阴影颜色
23+
@property (nonatomic,assign) CGSize shadowOffset; //阴影offset
24+
@property (nonatomic,assign) CGFloat shadowBlur; //阴影半径
25+
@property (nonatomic,assign) BOOL underLineForLink; //链接是否带下划线
26+
@property (nonatomic,assign) BOOL autoDetectLinks; //自动检测
27+
@property (nonatomic,assign) NSInteger numberOfLines; //行数
28+
@property (nonatomic,assign) CTTextAlignment textAlignment; //文字排版样式
29+
@property (nonatomic,assign) CTLineBreakMode lineBreakMode; //LineBreakMode
30+
@property (nonatomic,assign) CGFloat lineSpacing; //行间距
31+
@property (nonatomic,assign) CGFloat paragraphSpacing; //段间距
32+
@property (nonatomic,copy,nullable) NSString *text; //普通文本
33+
@property (nonatomic,copy,nullable) NSAttributedString *attributedText; //属性文本
34+
@property (nonatomic,assign) NSUInteger maxSyncDetectLength; //UI 线程做 link 检查的文字最大长度
3435

3536

3637

M80AttributedLabel/M80AttributedLabel.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ - (void)commonInit
8686
_autoDetectLinks = YES;
8787
_lineSpacing = 0.0;
8888
_paragraphSpacing = 0.0;
89+
_maxSyncDetectLength = 100;
8990

9091
if (self.backgroundColor == nil)
9192
{
@@ -1055,7 +1056,7 @@ - (void)recomputeLinksIfNeeded
10551056
{
10561057
return;
10571058
}
1058-
BOOL sync = length <= M80MinAsyncDetectLinkLength;
1059+
BOOL sync = length <= self.maxSyncDetectLength;
10591060
[self computeLink:text
10601061
sync:sync];
10611062
}
@@ -1064,8 +1065,8 @@ - (void)computeLink:(NSString *)text
10641065
sync:(BOOL)sync
10651066
{
10661067
__weak typeof(self) weakSelf = self;
1067-
typedef void (^LinkBlock) (NSArray *);
1068-
LinkBlock block = ^(NSArray *links)
1068+
typedef void (^M80LinkBlock) (NSArray *);
1069+
M80LinkBlock block = ^(NSArray *links)
10691070
{
10701071
weakSelf.linkDetected = YES;
10711072
if ([links count])

M80AttributedLabel/M80AttributedLabelDefines.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ typedef NS_OPTIONS(NSUInteger, M80ImageAlignment) {
3131

3232
typedef NSArray * _Nullable (^M80CustomDetectLinkBlock)(NSString * _Nullable text);
3333

34-
//如果文本长度小于这个值,直接在UI线程做Link检测,否则都dispatch到共享线程
35-
#define M80MinAsyncDetectLinkLength 50
36-
3734
NS_ASSUME_NONNULL_END
3835

3936
#endif

M80AttributedLabel/M80AttributedLabelURL.m

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88

99
#import "M80AttributedLabelURL.h"
1010

11-
static NSString *M80URLExpression = @"((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((:[0-9]+)?)((?:\\/[\\+~%\\/\\.\\w\\-]*)?\\??(?:[\\-\\+=&;%@\\.\\w]*)#?(?:[\\.\\!\\/\\\\\\w]*))?)";
12-
1311
static M80CustomDetectLinkBlock customDetectBlock = nil;
1412

15-
static NSString *M80URLExpressionKey = @"M80URLExpressionKey";
16-
17-
1813
@implementation M80AttributedLabelURL
1914

2015
+ (M80AttributedLabelURL *)urlWithLinkData:(id)linkData
@@ -42,11 +37,11 @@ + (NSArray *)detectLinks:(NSString *)plainText
4237
if ([plainText length])
4338
{
4439
links = [NSMutableArray array];
45-
NSRegularExpression *urlRegex = [M80AttributedLabelURL urlExpression];
46-
[urlRegex enumerateMatchesInString:plainText
40+
NSDataDetector *detector = [M80AttributedLabelURL linkDetector];
41+
[detector enumerateMatchesInString:plainText
4742
options:0
4843
range:NSMakeRange(0, [plainText length])
49-
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
44+
usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
5045
NSRange range = result.range;
5146
NSString *text = [plainText substringWithRange:range];
5247
M80AttributedLabelURL *link = [M80AttributedLabelURL urlWithLinkData:text
@@ -59,20 +54,23 @@ + (NSArray *)detectLinks:(NSString *)plainText
5954
}
6055
}
6156

62-
+ (NSRegularExpression *)urlExpression
57+
+ (NSDataDetector *)linkDetector
6358
{
59+
static NSString *M80LinkDetectorKey = @"M80LinkDetectorKey";
60+
6461
NSMutableDictionary *dict = [[NSThread currentThread] threadDictionary];
65-
NSRegularExpression *exp = dict[M80URLExpressionKey];
66-
if (exp == nil)
62+
NSDataDetector *detector = dict[M80LinkDetectorKey];
63+
if (detector == nil)
6764
{
68-
exp = [NSRegularExpression regularExpressionWithPattern:M80URLExpression
69-
options:NSRegularExpressionCaseInsensitive
70-
error:nil];
71-
dict[M80URLExpressionKey] = exp;
65+
66+
detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber
67+
error:nil];
68+
dict[M80LinkDetectorKey] = detector;
7269
}
73-
return exp;
70+
return detector;
7471
}
7572

73+
7674
+ (void)setCustomDetectMethod:(M80CustomDetectLinkBlock)block
7775
{
7876
customDetectBlock = [block copy];

OCDemo/Classes/TextTableViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ - (void)onRefresh:(id)sender
123123
[messages addObject:message];
124124
}
125125
dispatch_async(dispatch_get_main_queue(), ^{
126-
[_messages insertObjects:messages
126+
[self.messages insertObjects:messages
127127
atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [messages count])]];
128128
[self.refreshControl endRefreshing];
129129
[self.tableView reloadData];

0 commit comments

Comments
 (0)