This repository was archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathSAMTextView.m
More file actions
170 lines (119 loc) · 3.76 KB
/
Copy pathSAMTextView.m
File metadata and controls
170 lines (119 loc) · 3.76 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// SAMTextView.m
// SAMTextView
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2014 Sam Soffes. All rights reserved.
//
#import "SAMTextView.h"
@implementation SAMTextView
#pragma mark - Accessors
@synthesize attributedPlaceholder = _attributedPlaceholder;
- (void)setText:(NSString *)string {
[super setText:string];
[self setNeedsDisplay];
}
- (void)insertText:(NSString *)string {
[super insertText:string];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText {
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
- (void)setPlaceholder:(NSString *)string {
if ([string isEqualToString:self.attributedPlaceholder.string]) {
return;
}
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
if ([self isFirstResponder] && self.typingAttributes) {
[attributes addEntriesFromDictionary:self.typingAttributes];
} else {
attributes[NSFontAttributeName] = self.font;
attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:199.0/255.0 green:199.0/255.0 blue:205.0/255.0 alpha:1.0];
if (self.textAlignment != NSTextAlignmentLeft) {
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = self.textAlignment;
attributes[NSParagraphStyleAttributeName] = paragraph;
}
}
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:string attributes:attributes];
}
- (NSString *)placeholder {
return self.attributedPlaceholder.string;
}
- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder {
if ([_attributedPlaceholder isEqualToAttributedString:attributedPlaceholder]) {
return;
}
_attributedPlaceholder = attributedPlaceholder;
[self setNeedsDisplay];
}
- (void)setContentInset:(UIEdgeInsets)contentInset {
[super setContentInset:contentInset];
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font {
[super setFont:font];
[self setNeedsDisplay];
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment {
[super setTextAlignment:textAlignment];
[self setNeedsDisplay];
}
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}
#pragma mark - UIView
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
// Draw placeholder if necessary
if (self.text.length == 0 && self.attributedPlaceholder) {
CGRect placeholderRect = [self placeholderRectForBounds:self.bounds];
[self.attributedPlaceholder drawInRect:placeholderRect];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
// Redraw placeholder text when the layout changes if necessary
if (self.attributedPlaceholder && self.text.length == 0) {
[self setNeedsDisplay];
}
}
#pragma mark - Placeholder
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
CGRect rect = UIEdgeInsetsInsetRect(bounds, self.contentInset);
if ([self respondsToSelector:@selector(textContainer)]) {
rect = UIEdgeInsetsInsetRect(rect, self.textContainerInset);
CGFloat padding = self.textContainer.lineFragmentPadding;
rect.origin.x += padding;
rect.size.width -= padding * 2.0f;
} else {
if (self.contentInset.left == 0.0f) {
rect.origin.x += 8.0f;
}
rect.origin.y += 8.0f;
}
return rect;
}
#pragma mark - Private
- (void)initialize {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:self];
}
- (void)textChanged:(NSNotification *)notification {
[self setNeedsDisplay];
}
@end