一、UITextFiled和UITextView很像,区别是前一个是只显示一行文本(即使打了回车,可只是显示一行),后一个可显示多行文本。两个的delegate方法也很像.
但是UITextFiled有一个属性placeholder,即文本框的提示信息。而UITextView则没有。一般当点击文本框的时候都需要将默认的提示信息去掉,将光标移动到开始位置。
但是对于UITextView则没有placeholder这个属性,可以直接设置textView.text = @"请您输入电话号码". 清除默认的text有几种方法
1.在UITextView上添加一个UILabel,再在-(void)textviewDidChanged:(UITextView*)textView方法中移除掉这个Label,[label removeFromSuperView];
2.继承UITextView,在drawRect中添加或者删除placeholder: 参考http://stackoverflow.com/questions/1328638/placeholder-in-uitextview
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
|
SSTextView.h
//
// SSTextView.h
// SSToolkit
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
/**
UITextView subclass that adds placeholder support like UITextField has.
*/
@interface
SSTextView : UITextView
/**
The string that is displayed when there is no other text in the text view.
The default value is `nil`.
*/
@property
(
nonatomic
, retain)
NSString
*placeholder;
/**
The color of the placeholder.
The default is `[UIColor lightGrayColor]`.
*/
@property
(
nonatomic
, retain) UIColor *placeholderColor;
@end
SSTextView.m
//
// SSTextView.m
// SSToolkit
//
// Created by Sam Soffes on 8/18/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSTextView.h"
@interface
SSTextView ()
- (
void
)_initialize;
- (
void
)_updateShouldDrawPlaceholder;
- (
void
)_textChanged:(
NSNotification
*)notification;
@end
@implementation
SSTextView {
BOOL
_shouldDrawPlaceholder;
}
#pragma mark - Accessors
@synthesize
placeholder = _placeholder;
@synthesize
placeholderColor = _placeholderColor;
- (
void
)setText:(
NSString
*)string {
[
super
setText:string];
[
self
_updateShouldDrawPlaceholder];
}
- (
void
)setPlaceholder:(
NSString
*)string {
if
([string isEqual:_placeholder]) {
return
;
}
[_placeholder release];
_placeholder = [string retain];
[
self
_updateShouldDrawPlaceholder];
}
#pragma mark - NSObject
- (
void
)dealloc {
[[
NSNotificationCenter
defaultCenter] removeObserver:
self
name:UITextViewTextDidChangeNotification object:
self
];
[_placeholder release];
[_placeholderColor release];
[
super
dealloc];
}
#pragma mark - UIView
- (
id
)initWithCoder:(
NSCoder
*)aDecoder {
if
((
self
= [
super
initWithCoder:aDecoder])) {
[
self
_initialize];
}
return
self
;
}
- (
id
)initWithFrame:(CGRect)frame {
if
((
self
= [
super
initWithFrame:frame])) {
[
self
_initialize];
}
return
self
;
}
- (
void
)drawRect:(CGRect)rect {
[
super
drawRect:rect];
if
(_shouldDrawPlaceholder) {
[_placeholderColor set];
[_placeholder drawInRect:CGRectMake(8.0f, 8.0f,
self
.frame.size.width - 16.0f,
self
.frame.size.height - 16.0f) withFont:
self
.font];
}
}
#pragma mark - Private
- (
void
)_initialize {
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(_textChanged:) name:UITextViewTextDidChangeNotification object:
self
];
self
.placeholderColor = [UIColor colorWithWhite:0.702f alpha:1.0f];
_shouldDrawPlaceholder =
NO
;
}
- (
void
)_updateShouldDrawPlaceholder {
BOOL
prev = _shouldDrawPlaceholder;
_shouldDrawPlaceholder =
self
.placeholder &&
self
.placeholderColor &&
self
.text.length == 0;
if
(prev != _shouldDrawPlaceholder) {
[
self
setNeedsDisplay];
}
}
- (
void
)_textChanged:(
NSNotification
*)notificaiton {
[
self
_updateShouldDrawPlaceholder];
}
@end
|
3.还是使用UITextView的delegate方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
- (
BOOL
) textViewShouldBeginEditing:(UITextView *)textView
{
if
(textView.tag == 0) {
textView.text = @
""
;
textView.textColor = [UIColor blackColor];
textView.tag = 1;
}
return
YES
;
}
- (
void
)textViewDidChange:(UITextView *)textView
{
if
([textView.text length] == 0)
{
textView.text = @
"Foobar placeholder"
;
textView.textColor = [UIColor lightGrayColor];
textView.tag = 0;
}
}
|
二、如果希望一进入Controller页面,就显示键盘,可以在viewWillAppear或者viewDidAppear中 添加[textFiled becomeFirstResponser]