-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFUITextView.m
150 lines (124 loc) · 2.81 KB
/
RFUITextView.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
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
//
// RFUITextView.m
// XiaoXunTong
//
// Created by gouzhehua on 14-8-9.
// Copyright (c) 2014年 GZH. All rights reserved.
//
#import "RFUITextView.h"
#import "RFKit.h"
@interface RFUITextView ()
<
UITextViewDelegate
>
@end
@implementation RFUITextView
@synthesize tvInput = _tvInput;
@synthesize blockShouldBeginEditing = _blockShouldBeginEditing;
@synthesize blockShouldReturn = _blockShouldReturn;
@synthesize paddingLeft = _paddingLeft;
@synthesize paddingTop = _paddingTop;
@synthesize isReturnToResign = _isReturnToResign;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.tvInput = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectZero];
_paddingLeft = 5;
_paddingTop = 5;
[self configUI];
}
return self;
}
- (void)dealloc
{
_tvInput.delegate = nil;
SAFE_ARC_RELEASE(_tfInput);
self.blockShouldBeginEditing = nil;
self.blockShouldReturn = nil;
SAFE_ARC_SUPER_DEALLOC();
}
- (void)awakeFromNib
{
self.tvInput = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectZero];
_paddingLeft = 5;
_paddingTop = 5;
[self configUI];
}
- (void)configUI
{
CGFloat offsetY = 0;
if (IS_IOS7) offsetY = 0;
CGRect vRect = self.frame;
CGRect tvRect = self.frame;
tvRect.origin.x = _paddingLeft;
tvRect.origin.y = _paddingTop;
tvRect.size.width = MAX((vRect.size.width - _paddingLeft*2), 0);
tvRect.size.height = MAX((vRect.size.height - _paddingTop*2), 0);
_tvInput.frame = tvRect;
self.backgroundColor = [UIColor whiteColor];
[self addSubview:_tvInput];
_tvInput.backgroundColor = [UIColor clearColor];
_tvInput.textColor = [UIColor blackColor];
_tvInput.font = [UIFont systemFontOfSize:16];
_tvInput.delegate = self;
_tvInput.scrollIndicatorInsets = UIEdgeInsetsZero;
_tvInput.contentInset = UIEdgeInsetsZero;
if (_isReturnToResign)
{
_tvInput.returnKeyType = UIReturnKeyDone;
}
}
- (BOOL)resignFirstResponder
{
return [_tvInput resignFirstResponder];
}
#pragma mark UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if (self.blockShouldBeginEditing != nil)
{
return self.blockShouldBeginEditing(self);
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
BOOL bRet = YES;
if (self.blockShouldReturn != nil)
{
bRet = self.blockShouldReturn(self);
}
if (bRet)
{
[textView resignFirstResponder];
}
return bRet;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (_isReturnToResign)
{
if (1 == range.length)
{
//按下退格键
return YES;
}
if ([text isEqualToString:@"\n"])
{
//按下return键
//这里隐藏键盘,不做任何处理
[textView resignFirstResponder];
return NO;
}
else
{
return YES;
}
return NO;
}
return YES;
}
@end