-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFUITextField.m
124 lines (100 loc) · 2.4 KB
/
RFUITextField.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
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
//
// RFUITextField.m
// XiaoXunTong
//
// Created by gouzhehua on 14-8-1.
// Copyright (c) 2014年 GZH. All rights reserved.
//
#import "RFUITextField.h"
#import "RFUIKit.h"
@interface RFUITextField ()
<
UITextFieldDelegate
>
{
}
- (void)configUI;
@end
@implementation RFUITextField
@synthesize tfInput = _tfInput;
@synthesize blockShouldBeginEditing = _blockShouldBeginEditing;
@synthesize blockShouldReturn = _blockShouldReturn;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.tfInput = [[UITextField alloc] initWithFrame:CGRectZero];
[self configUI];
}
return self;
}
- (void)dealloc
{
_tfInput.delegate = nil;
SAFE_ARC_RELEASE(_tfInput);
self.blockShouldBeginEditing = nil;
self.blockShouldReturn = nil;
SAFE_ARC_SUPER_DEALLOC();
}
- (void)awakeFromNib
{
self.tfInput = [[UITextField alloc] initWithFrame:Frame2Bounds(self.frame)];
[self configUI];
}
- (void)configUI
{
CGFloat offsetY = 0;
// if (IS_IOS7) offsetY = 0;
CGRect vRect = self.frame;
CGRect tfRect = self.frame;
tfRect.origin.x = 10;
tfRect.origin.y = MAX((vRect.size.height - 30) / 2 + offsetY, 0);
tfRect.size.width = MAX((vRect.size.width - 20), 0);
tfRect.size.height = 30;
_tfInput.frame = tfRect;
self.backgroundColor = [UIColor whiteColor];
[self addSubview:_tfInput];
_tfInput.backgroundColor = [UIColor clearColor];
_tfInput.textColor = [UIColor blackColor];
_tfInput.font = [UIFont systemFontOfSize:16];
_tfInput.borderStyle = UITextBorderStyleNone;
_tfInput.returnKeyType = UIReturnKeyDone;
_tfInput.delegate = self;
}
- (BOOL)resignFirstResponder
{
return [_tfInput resignFirstResponder];
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (self.blockShouldBeginEditing != nil)
{
return self.blockShouldBeginEditing(self);
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
BOOL bRet = YES;
if (self.blockShouldReturn != nil)
{
bRet = self.blockShouldReturn(self);
}
if (bRet)
{
[textField resignFirstResponder];
}
return bRet;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL bRet = YES;
if (self.blockShouldChangeCharactersInRange != nil)
{
bRet = self.blockShouldChangeCharactersInRange(self, range, string);
}
return bRet;
}
@end