Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMTextView.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0961208e2dd1a70d743613d1e33cbda9d69e03e4 (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
117
118
119
120
121
#import "MWMTextView.h"
#import "MWMCommon.h"

@interface MWMTextView ()

@property (nonatomic, readwrite) UILabel * placeholderView;

@end

@implementation MWMTextView

- (void)awakeFromNib
{
  [super awakeFromNib];
  [self setTextContainerInset:UIEdgeInsetsZero];

  [self updatePlaceholderVisibility];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:)
                        name:UITextViewTextDidChangeNotification object:self];
  self.clipsToBounds = YES;
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

- (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