-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAFBlockAttributedLabel.m
More file actions
134 lines (105 loc) · 4.26 KB
/
AFBlockAttributedLabel.m
File metadata and controls
134 lines (105 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// AFBlockAttributedLabel.m
// AFBlockAttributedLabel
//
// Created by Daniel Kuhnke on 27.02.15.
// Copyright (c) 2015 appfarms GmbH & Co. KG. All rights reserved.
//
#import "AFBlockAttributedLabel.h"
#define kDidSelectLinkWithURLBlockKey @"kDidSelectLinkWithURLBlockKey"
@interface AFBlockAttributedLabel () <TTTAttributedLabelDelegate>
@property (nonatomic, strong) NSMutableDictionary * blocks;
@end
@implementation AFBlockAttributedLabel
- (void) setHTML:(NSString *)html
{
[self setHTML:html andDidSelectLinkWithURLBlock:nil];
}
- (void) setHTML:(NSString *)html andDidSelectLinkWithURLBlock:(void(^)(NSURL * url))block
{
[self setHTML:html linkAttributes:[AFBlockAttributedLabel defaultLinkAttributes] activeLinkAttributes:[AFBlockAttributedLabel defaultActiveLinkAttributes] andDidSelectLinkWithURLBlock:block];
}
- (void) setHTML:(NSString *)html linkAttributes:(NSDictionary *)linkAttributes activeLinkAttributes:(NSDictionary *)activeLinkAttributes andDidSelectLinkWithURLBlock:(void(^)(NSURL * url))block
{
self.numberOfLines = 0;
self.enabledTextCheckingTypes = NSTextCheckingTypeLink;
self.linkAttributes = linkAttributes;
self.activeLinkAttributes = activeLinkAttributes;
self.text = [self attributedStringByHTML:html];
[self setDidSelectLinkWithURLBlock:block];
}
+ (NSMutableAttributedString *) attributedStringByHTML:(NSString *)html withFont:(UIFont *)font andTextColor:(UIColor *)textColor
{
if (!html || [html isKindOfClass:[NSString class]] == NO)
{
html = @"";
}
if (!font || [font isKindOfClass:[UIFont class]] == NO)
{
font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
}
if (!textColor || [textColor isKindOfClass:[UIColor class]] == NO)
{
textColor = [UIColor blackColor];
}
NSDictionary * documentAttributes = nil;
NSError * error = nil;
CGFloat r, g, b, a;
[textColor getRed:&r green:&g blue:&b alpha:&a];
html = [NSString stringWithFormat:@"<html><head><style>body { font-family: '%@'; font-size:%fpx; margin: 0px; padding: 0px; color: rgb(%d,%d,%d); }</style></head><body>%@</body></html>", font.fontName, font.pointSize, (int)(r*255), (int)(g*255), (int)(b*255), html];
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) }
documentAttributes:&documentAttributes
error:&error];
return (string && [string isKindOfClass:[NSMutableAttributedString class]]) ? string : nil;
}
- (NSMutableAttributedString *) attributedStringByHTML:(NSString *)html
{
return [AFBlockAttributedLabel attributedStringByHTML:html
withFont:self.font
andTextColor:self.textColor];
}
#pragma mark - Getter
- (NSMutableDictionary *)blocks
{
if (!_blocks || [_blocks isKindOfClass:[NSMutableDictionary class]] == NO)
{
_blocks = [NSMutableDictionary dictionary];
}
return _blocks;
}
#pragma mark - Setter
- (void) setDidSelectLinkWithURLBlock:(void(^)(NSURL * url))block
{
self.delegate = self;
if (block)
{
self.blocks[kDidSelectLinkWithURLBlockKey] = [block copy];
}
else
{
[self.blocks removeObjectForKey:kDidSelectLinkWithURLBlockKey];
}
}
#pragma mark - TTTAttributedLabelDelegate
- (void) attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
void(^block)(NSURL * url);
block = self.blocks[kDidSelectLinkWithURLBlockKey];
if (block)
{
block(url);
}
}
#pragma mark - Static
+ (NSDictionary *) defaultLinkAttributes
{
return @{ NSForegroundColorAttributeName: [UIColor orangeColor],
NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
}
+ (NSDictionary *) defaultActiveLinkAttributes
{
return @{ NSForegroundColorAttributeName: [UIColor orangeColor],
NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
}
@end