step —1:UITextField
1、基本用法
1.声明
@property(nonatomic,strong) UITextField*textField;
2.准守代理
3.创建
self.textField= [[UITextFieldalloc] init];
4.设置属性
//代理
self.textField.delegate= self;
self.textField.frame= CGRectMake(20, 200, 200, 30);
//显示字体
self.textField.text= @"txt";
//字体颜色
self.textField.textColor= [UIColorblueColor];
//字体大小
self.textField.font= [UIFontsystemFontOfSize:16];
//字体对齐方式
self.textField.textAlignment= NSTextAlignmentCenter;
/*边框样式
UITextBorderStyleNone, 默认
UITextBorderStyleLine, 直线
UITextBorderStyleBezel,阴影
UITextBorderStyleRoundedRect 圆角
*/
self.textField.borderStyle= UITextBorderStyleLine;
//默认提示语
self.textField.placeholder= @"Enter text ";
//再次编辑清空
self.textField.clearsOnBeginEditing= YES;
//自适应大小
self.textField.adjustsFontSizeToFitWidth= YES;
//最小字体
self.textField.minimumFontSize= 0.0;
//秘语输入
self.textField.secureTextEntry= YES;
/*清空按钮模式
UITextFieldViewModeNever, 无
UITextFieldViewModeWhileEditing, 编辑时候出现
UITextFieldViewModeUnlessEditing, 除了编辑以外出现
UITextFieldViewModeAlways 一直出现
*/
self.textField.clearButtonMode= UITextFieldViewModeWhileEditing;
/*return 类型
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
*/
self.textField.returnKeyType= UIReturnKeyDefault;
/*左视图
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
*/
UIImageView*leftView = [[UIImageViewalloc] init];
leftView.bounds= CGRectMake(0, 0, 30, 30);
leftView.image= [UIImageimageNamed:@"Image"];
self.textField.leftView= leftView;
self.textField.leftViewMode= UITextFieldViewModeAlways;
//背景图
self.textField.background= [UIImageimageNamed:@"icon"];
//自定义键盘头视图
UIView*headView = [[UIViewalloc] init];
headView.bounds= CGRectMake(0, 0, self.view.frame.size.width, 40);
headView.backgroundColor= [UIColoryellowColor];
//self.textField.inputAccessoryView = headView;
//自定义键盘视图
UIView*keyView = [[UIViewalloc] init];
keyView.bounds= CGRectMake(0, 0, self.view.frame.size.width, 40);
keyView.backgroundColor= [UIColorredColor];
//self.textField.inputView = keyView;
[self.textFieldplaceholderRectForBounds:CGRectMake(100, 10, 50, 20)];
/*重写光标 默认提示语 leftView位置
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
*/
5.添加视图
[self.viewaddSubview:self.textField];
6.代理方法
//控制当前输入框是否能被编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField{
NSLog(@"-0----%s",__func__);
returnYES;
}
//当输入框开始时触发 ( 获得焦点触发)
- (void)textFieldDidBeginEditing:(UITextField*)textField{
NSLog(@"-1----%s",__func__);
}
//询问输入框是否可以结束编辑 ( 键盘是否可以收回)
- (BOOL)textFieldShouldEndEditing:(UITextField*)textField{
NSLog(@"--4---%s",__func__);
returnYES;
}
//当前输入框结束编辑时触发 ( 键盘收回之后触发 )
- (void)textFieldDidEndEditing:(UITextField*)textField{
NSLog(@"-5----%s",__func__);
}
//当输入框文字发生变化时触发 ( 只有通过键盘输入时 , 文字改变 , 触发 )
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
NSLog(@"-3----%s",__func__);
returnYES;
}
//控制输入框清除按钮是否有效 (yes, 有 ;no, 没有)
- (BOOL)textFieldShouldClear:(UITextField*)textField{
NSLog(@"-2----%s",__func__);
returnYES;
}
//控制键盘是否回收
- (BOOL)textFieldShouldReturn:(UITextField*)textField{
[textField resignFirstResponder];
NSLog(@"-6----%s",__func__);
returnYES;
}
2、特殊用法(后续更新)
step —1:UITextView
1、基本用法
1.声明
@property(nonatomic,strong) UITextView*textView;
2.准守代理
3.创建
self.textView= [[UITextViewalloc] init];
4.设置属性
//代理
self.textView.delegate= self;
self.textView.frame= CGRectMake(20, 260, 200, 100);
self.textView.backgroundColor= [UIColorlightGrayColor];
//显示字体
self.textView.text= @"UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text,010 - 6591917 such as when displaying the body of a large text document.";
//字体颜色
self.textView.textColor= [UIColorwhiteColor];
//字体大小
self.textView.font= [UIFontsystemFontOfSize:16];
//字体对齐方式
self.textView.textAlignment= NSTextAlignmentLeft;
//是否可以选中
self.textView.selectable= YES;
//选择范围
self.textView.selectedRange= NSMakeRange(4, 3);
//滚动到文本的某个段落
[self.textViewscrollRangeToVisible: NSMakeRange(150, 3)];
//是否允许再次编辑插入内容
self.textView.clearsOnInsertion= NO;
/*设定使电话号码、网址、电子邮件和符合格式的日期等文字变为链接文字
UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection
UIDataDetectorTypeLink = 1 << 1, // URL detection
UIDataDetectorTypeAddress
*/
self.textView.dataDetectorTypes= UIDataDetectorTypeAll;
//return键的类型
self.textView.returnKeyType= UIReturnKeyDefault;
//键盘类型
self.textView.keyboardType= UIKeyboardTypeDefault;
//自定义键盘头视图
UIView*headView = [[UIViewalloc] init];
headView.bounds= CGRectMake(0, 0, self.view.frame.size.width, 40);
headView.backgroundColor= [UIColoryellowColor];
//self.textView.inputAccessoryView = headView;
//自定义键盘视图
UIView*keyView = [[UIViewalloc] init];
keyView.bounds= CGRectMake(0, 0, self.view.frame.size.width, 40);
keyView.backgroundColor= [UIColorredColor];
//self.textView.inputView = keyView;
5.添加视图
[self.viewaddSubview:self.textView];
6.代理方法
// 将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView*)textView{
NSLog(@"----%s",__func__);
returnYES;
}
// 将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView*)textView{
NSLog(@"----%s",__func__);
returnYES;
}
// 开始编辑
- (void)textViewDidBeginEditing:(UITextView*)textView{
NSLog(@"---%s",__func__);
}
// 结束编辑
- (void)textViewDidEndEditing:(UITextView*)textView{
NSLog(@"---%s",__func__);
}
// 文本将要改变
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{
NSLog(@"---%s",__func__);
//[NSString stringWithFormat:@"%lu/100",(unsigned long)range.location]; 字节统计
if(range.location>= 100){
return NO;
}elseif([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
returnNO;
}
returnYES;
}
// 文本发生改变
- (void)textViewDidChange:(UITextView*)textView{
NSLog(@"---%s",__func__);
}
// 焦点发生改变
- (void)textViewDidChangeSelection:(UITextView*)textView{
NSLog(@"----%s",__func__);
}
// 是否允许对文本中的URL进行操作
- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0){
NSLog(@"-----%s",__func__);
returnYES;
}
// 是否允许对文本中的富文本进行操作
- (BOOL)textView:(UITextView*)textView shouldInteractWithTextAttachment:(NSTextAttachment*)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0){
NSLog(@"----%s",__func__);
returnYES;
}
2、特殊用法(后续更新)
网友评论