-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSSDynamicTextView.m
More file actions
106 lines (78 loc) · 2.69 KB
/
SSDynamicTextView.m
File metadata and controls
106 lines (78 loc) · 2.69 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
//
// SSDynamicTextView.m
// SSDynamicText
//
// Created by Jonathan Hersh on 10/6/13.
// Copyright (c) 2013 Splinesoft. All rights reserved.
//
#import "SSDynamicTextView.h"
#import "SSDynamicTextSizeChanger.h"
@interface SSDynamicTextView ()
@property (nonatomic, strong) SSDynamicTextSizeChanger *textSizeChanger;
@end
@implementation SSDynamicTextView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self startObservingTextSizeChanges];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setupDefaultFontDescriptorBasedOnFont:self.font];
[self startObservingTextSizeChanges];
}
+ (instancetype)textViewWithFont:(NSString *)fontName baseSize:(CGFloat)size {
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:fontName size:size];
return [self textViewWithFontDescriptor:fontDescriptor];
}
+ (instancetype)textViewWithFontDescriptor:(UIFontDescriptor *)descriptor {
SSDynamicTextView *textView = [self new];
textView.defaultFontDescriptor = descriptor;
return textView;
}
- (void)dealloc {
[self ss_stopObservingTextSizeChanges];
}
#pragma mark - Accessors
- (void)setDefaultFontDescriptor:(UIFontDescriptor *)defaultFontDescriptor {
self.textSizeChanger.defaultFontDescriptor = defaultFontDescriptor;
[super setDefaultFontDescriptor:defaultFontDescriptor];
}
- (void)setFont:(UIFont *)font {
[super setFont:font];
[self setupDefaultFontDescriptorBasedOnFont:self.font];
}
#pragma mark - Private Methods
- (SSDynamicTextSizeChanger *)textSizeChanger {
if (_textSizeChanger == nil) {
_textSizeChanger = [self createTextChanger];
}
return _textSizeChanger;
}
- (SSDynamicTextSizeChanger *)createTextChanger {
SSDynamicTextSizeChanger *changer = [[SSDynamicTextSizeChanger alloc] init];
__weak typeof(self) weakSelf = self;
changer.fontChangeBlock = ^(UIFont *font) {
// https://github.com/splinesoft/SSDynamicText/issues/40
[weakSelf superSetFont:font];
};
changer.attributedTextChangeBlock = ^(NSAttributedString *attributedText) {
weakSelf.attributedText = attributedText;
};
return changer;
}
- (void)superSetFont:(UIFont *)font {
[super setFont:font];
}
- (void)startObservingTextSizeChanges {
[self ss_startObservingTextSizeChangesWithBlock:self.textSizeChanger.changeHandler];
}
#pragma mark - SSDynamicAttributedTextSizable
- (NSAttributedString *)dynamicAttributedText {
return self.textSizeChanger.dynamicAttributedText;
}
- (void)setDynamicAttributedText:(NSAttributedString *)attributedText {
self.textSizeChanger.dynamicAttributedText = attributedText;
}
@end