-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFuASTextNode.m
114 lines (91 loc) · 4.73 KB
/
FuASTextNode.m
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
//
// FuASTextNode.m
// MiaoMiaoZheApp
//
// Created by 付海龙 on 2017/12/19.
// Copyright © 2017年 付海龙. All rights reserved.
//
#import "FuASTextNode.h"
@implementation FuASTextNode
- (id)init
{
self = [super init];
if (self) {
self.lineSpacing = 0.f;
self.kern = 0.f;
self.textAlignment = NSTextAlignmentLeft;
}
return self;
}
- (void)addTextLink:(NSString *)string font:(UIFont *)font color:(UIColor *)color block:(NSMutableAttributedString * (^)(NSMutableAttributedString *mutableAttributedString))block
{
NSDictionary *(^attributes)(UIFont *, UIColor *) = ^(UIFont *font, UIColor *color){
NSMutableDictionary *mutDict = [NSMutableDictionary dictionary];
if (color) {
[mutDict setObject:color forKey:NSForegroundColorAttributeName];
}
if (font) {
[mutDict setObject:font forKey:NSFontAttributeName];
}
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:mutDict];
[mutableAttributes setObject:@(self.kern) forKey:NSKernAttributeName];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = self.lineSpacing;
if (self.maximumNumberOfLines == 1) {
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
}
paragraphStyle.alignment = self.textAlignment;
[mutableAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
return [NSDictionary dictionaryWithDictionary:mutableAttributes];
};
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes(font, color)];
if (block) {
mutableAttributedString = block(mutableAttributedString);
}
self.attributedText = mutableAttributedString;
}
- (void)setLineSpacing:(CGFloat)lineSpacing
{
_lineSpacing = lineSpacing;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSMutableDictionary *mutAttributes = [NSMutableDictionary dictionaryWithDictionary:mutableAttributedString.attributes];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = lineSpacing;
[mutAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[mutableAttributedString addAttributes:mutAttributes range:NSMakeRange(0, self.attributedText.string.length)];
self.attributedText = mutableAttributedString;
}
- (void)setKern:(CGFloat)kern
{
_kern = kern;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSMutableDictionary *mutAttributes = [NSMutableDictionary dictionaryWithDictionary:mutableAttributedString.attributes];
[mutAttributes setObject:@(kern) forKey:NSKernAttributeName];
[mutableAttributedString addAttributes:mutAttributes range:NSMakeRange(0, self.attributedText.string.length)];
self.attributedText = mutableAttributedString;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
_textAlignment = textAlignment;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSMutableDictionary *mutAttributes = [NSMutableDictionary dictionaryWithDictionary:mutableAttributedString.attributes];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = textAlignment;
[mutAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[mutableAttributedString addAttributes:mutAttributes range:NSMakeRange(0, self.attributedText.string.length)];
self.attributedText = mutableAttributedString;
}
- (void)setMaximumNumberOfLines:(NSUInteger)maximumNumberOfLines
{
super.maximumNumberOfLines = maximumNumberOfLines;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSMutableDictionary *mutAttributes = [NSMutableDictionary dictionaryWithDictionary:mutableAttributedString.attributes];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
if (maximumNumberOfLines == 1) {
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
}
[mutAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[mutableAttributedString addAttributes:mutAttributes range:NSMakeRange(0, self.attributedText.string.length)];
self.attributedText = mutableAttributedString;
}
@end