forked from soffes/sstoolkit
/
SSTextField.m
79 lines (56 loc) · 1.76 KB
/
SSTextField.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
//
// SSTextField.m
// SSToolkit
//
// Created by Sam Soffes on 3/11/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSTextField.h"
#import "SSDrawingUtilities.h"
@implementation SSTextField
#pragma mark - Accessors
@synthesize textEdgeInsets = _textEdgeInsets;
@synthesize clearButtonEdgeInsets = _clearButtonEdgeInsets;
@synthesize placeholderTextColor = _placeholderTextColor;
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor {
[placeholderTextColor retain];
[_placeholderTextColor release];
_placeholderTextColor = placeholderTextColor;
if (!self.text && self.placeholder) {
[self setNeedsDisplay];
}
}
#pragma mark - NSObject
- (void)dealloc {
[_placeholderTextColor release];
[super dealloc];
}
#pragma mark - UIView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_textEdgeInsets = UIEdgeInsetsZero;
_clearButtonEdgeInsets = UIEdgeInsetsZero;
}
return self;
}
#pragma mark - UITextField
- (CGRect)textRectForBounds:(CGRect)bounds {
return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], _textEdgeInsets);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
CGRect rect = [super clearButtonRectForBounds:bounds];
rect = CGRectSetY(rect, rect.origin.y + _clearButtonEdgeInsets.top);
return CGRectSetX(rect, rect.origin.x + _clearButtonEdgeInsets.right);
}
- (void)drawPlaceholderInRect:(CGRect)rect {
if (!_placeholderTextColor) {
[super drawPlaceholderInRect:rect];
return;
}
[_placeholderTextColor setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}
@end