blob: b30af912f1dcc646845e9b44db3de531ba6f6a66 (
plain)
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
|
#import "MWMTextView.h"
#import "Common.h"
@interface MWMTextView ()
@property (nonatomic, readwrite) UILabel * placeholderView;
@end
@implementation MWMTextView
- (void)awakeFromNib
{
[self setTextContainerInset:UIEdgeInsetsZero];
[self updatePlaceholderVisibility];
NSNotificationCenter * defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(textDidChange:)
name:UITextViewTextDidChangeNotification object:self];
self.clipsToBounds = YES;
}
- (UILabel *)placeholderView
{
if (!_placeholderView)
{
_placeholderView = [[UILabel alloc] initWithFrame:self.bounds];
_placeholderView.opaque = NO;
_placeholderView.backgroundColor = [UIColor clearColor];
_placeholderView.textColor = [UIColor lightGrayColor];
_placeholderView.textAlignment = self.textAlignment;
_placeholderView.userInteractionEnabled = NO;
_placeholderView.font = self.font;
_placeholderView.isAccessibilityElement = NO;
_placeholderView.numberOfLines = 0;
}
return _placeholderView;
}
#pragma mark - Setters
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder.copy;
self.placeholderView.text = placeholder;
[self resizePlaceholderFrame];
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.placeholderView.font = font;
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self updatePlaceholderVisibility];
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self updatePlaceholderVisibility];
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
[super setTextAlignment:textAlignment];
self.placeholderView.textAlignment = textAlignment;
}
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
[super setTextContainerInset:textContainerInset];
[self updatePlaceholderInset:textContainerInset];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self resizePlaceholderFrame];
}
- (void)resizePlaceholderFrame
{
[self.placeholderView sizeToFit];
}
- (void)textDidChange:(NSNotification *)aNotification
{
[self updatePlaceholderVisibility];
}
- (void)updatePlaceholderInset:(UIEdgeInsets)inset
{
CGFloat const kDefaultPlaceholderLeftInset = 5.0;
self.placeholderView.frame = CGRectMake(inset.left + kDefaultPlaceholderLeftInset, inset.top, self.bounds.size.width - inset.right, self.bounds.size.height - inset.bottom);
[self resizePlaceholderFrame];
}
- (void)updatePlaceholderVisibility
{
if (!self.text.length)
{
[self addSubview:self.placeholderView];
[self sendSubviewToBack:self.placeholderView];
}
else
{
[self.placeholderView removeFromSuperview];
}
}
@end
|