UI 学习 第二章 UItextfield UItextView
UITextField
1.创建
UITextField
*filed = [[
UITextField
alloc
]
initWithFrame
:
CGRectMake
(
20
,
30
,
300
,
30
)];
2.设置背景色
filed.backgroundColor = [UIColor redColor];
3.设置边框风格
filed.
borderStyle
=
UITextBorderStyleRoundedRect
;
4.输入提示
filed.
placeholder
=
@"
请输入用户名
"
;
5.开始编辑下一个时这个清零
filed.
clearsOnBeginEditing
=
YES
;
6.右边小清空按钮显示样式
filed.
clearButtonMode
=
UITextFieldViewModeWhileEditing
;
7.密码显示样式
filed.
secureTextEntry
=
YES
;
9.设置返回键样式
filed.
returnKeyType
=
UIReturnKeyGo
;
10.设置左右两边小视图
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 20, 20)];
view.backgroundColor = [UIColor greenColor];
filed.leftView = view;
filed.leftViewMode = UITextFieldViewModeAlways;
11.设置代理
filed.
delegate
=
self
;
以下方法需遵守协议
12.开始编辑时调用
-(
void
)textFieldDidBeginEditing:(
UITextField
*)textField{
NSLog ( @" 开始编辑 " );
NSLog ( @" 开始编辑 " );
}
13
.判断输入的字符串
-(
BOOL
)textField:(
UITextField
*)textField shouldChangeCharactersInRange:(
NSRange
)range replacementString:(
NSString
*)string{
NSLog ( @" 输入的是 %@" ,string);
return YES ;
NSLog ( @" 输入的是 %@" ,string);
return YES ;
}
14.当收回键盘时调用:
-(
void
)textFieldDidEndEditing:(
UITextField
*)textField{
NSLog ( @" 结束编辑 " );
NSLog ( @" 结束编辑 " );
}
15.当点return键时调用
-(
BOOL
)textFieldShouldReturn:(
UITextField
*)textField{
NSLog ( @"hi0h00i" );
[textField resignFirstResponder ];
return YES ;
NSLog ( @"hi0h00i" );
[textField resignFirstResponder ];
return YES ;
}
16.点击屏幕空白处收回键盘时调用
-(
void
)touchesBegan:(
NSSet
*)touches withEvent:(
UIEvent
*)event{
NSLog ( @" 键盘消失 " );
[ self . view endEditing : YES ];
NSLog ( @" 键盘消失 " );
[ self . view endEditing : YES ];
}
UITextView
1.创建
UITextView
*textView = [[
UITextView
alloc
]
initWithFrame
:
CGRectMake
(
20
,
20
,
100
,
100
)];
2.背景色
textView.
backgroundColor
= [
UIColor
grayColor
];
3.是否允许被编辑
textView.
editable
=
YES
;
4.是否允许滚动
textView.
scrollEnabled
=
YES
;
5.弹簧效果
textView.
bounces
=
YES
;
6.边框颜色
textView.
layer
.
borderColor
= [
UIColor
redColor
].
CGColor
;
7.边框粗细
textView.
layer
.
borderWidth
=
1
;
8.设置圆角,如果想要圆形,则设置为高的一半
textView.
layer
.
cornerRadius
=
30
;
9.设置return键类型
textView.
returnKeyType
=
UIReturnKeyDone
;
方法:
10.判断输入的文本内容(单个)
-(
BOOL
)textView:(
UITextView
*)textView shouldChangeTextInRange:(
NSRange
)range replacementText:(
NSString
*)text{
if ([text isEqualToString : @"\n" ]) {
[textView resignFirstResponder ];(当输入的是return时收回键盘)
}
NSLog ( @"uobhohuohno" );
return YES ;
if ([text isEqualToString : @"\n" ]) {
[textView resignFirstResponder ];(当输入的是return时收回键盘)
}
NSLog ( @"uobhohuohno" );
return YES ;
}
11.判断文本内的内容(整个)
-(
void
)textViewDidChange:(
UITextView
*)textView{
NSLog ( @"%@" ,textView. text );
NSLog ( @"%@" ,textView. text );
}