UITextView和UITextField最大的区别是:UITextView支持多行输入,而UITextField只能单行输入。实际上,UITextView继承自UIScrollView ,UITextField继承自UIView。在使用上我们完全可以把UITextView看作是UITextField的加强版。
中国.jpgUITextField(输入框)
/*
command + k 显示或隐藏模拟器键盘
*/
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>//遵守协议
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*********UITextField(输入框)**********/
//UITextField->UIControl->UIView
// UITextView和UITextField是有区别的
/*
1、创建
2、背景颜色
3、添加
*/
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 260, 40)];
//textField.backgroundColor = [UIColor grayColor];
//colorWithPatternImage 将图片对象转成颜色对象
//平铺的效果
//textField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"q.jpg"]];
//边框类型:borderStyle
/*
1、UITextBorderStyleNone 无
2、UITextBorderStyleRoundedRect 圆角
3、UITextBorderStyleLine 直角 黑色
4、UITextBorderStyleBezel 直角 灰色
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//文字颜色:textColor
textField.textColor = [UIColor redColor];
//字体大小:font
textField.font = [UIFont systemFontOfSize:23.0];
//对齐方式:textAlignment 水平方向对齐
textField.textAlignment = NSTextAlignmentLeft;
//对齐方式:垂直方向 contentVerticalAlignment
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//自适应文字大小:adjustsFontSizeToFitWidth
textField.adjustsFontSizeToFitWidth = YES;
//最小字体的设置:minimumFontSize
textField.minimumFontSize = 23.0;
//键盘颜色:keyboardAppearance 翻译:键盘的外观
/*
1、UIKeyboardAppearanceAlert=UIKeyboardAppearanceDark 黑色
2、UIKeyboardAppearanceDefault=UIKeyboardAppearanceLight 白色
*/
textField.keyboardAppearance = UIKeyboardAppearanceLight;
//键盘样式:keyboardType
/*
1、UIKeyboardTypeAlphabet 符号键盘
2、UIKeyboardTypeDecimalPad 数字键盘
3、UIKeyboardTypePhonePad 数字键盘
*/
textField.keyboardType = UIKeyboardTypeDefault;
//return键样式
/*
1、UIReturnKeyDone Done
2、UIReturnKeyGoogle Search
*/
textField.returnKeyType = UIReturnKeyGoogle;
//自动大写:autocapitalizationType
/*
1、UITextAutocapitalizationTypeAllCharacters 所有字母都大写
2、UITextAutocapitalizationTypeNone 无
3、UITextAutocapitalizationTypeSentences 每个句子的首字母大写
4、UITextAutocapitalizationTypeWords 每个单词的首字母大写
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
//是否自动纠错:autocorrectionType
/*
1、UITextAutocorrectionTypeDefault 默认
2、UITextAutocorrectionTypeYes 纠错
3、UITextAutocorrectionTypeNo 不纠错
*/
textField.autocorrectionType = UITextAutocorrectionTypeNo;
//提示文字:placeholder
textField.placeholder = @"请输入账号";
//密文效果:secureTextEntry
textField.secureTextEntry = NO;
//背景图片:background 边框状态为圆角:无效
textField.background = [UIImage imageNamed:@"1.png"];
//一键删除的按钮:出现状态
/*
1、UITextFieldViewModeAlways 一直出现
2、UITextFieldViewModeNever 一直没有 默认效果
3、UITextFieldViewModeWhileEditing 当编辑时出现,不编辑时消失
4、UITextFieldViewModeUnlessEditing 当编辑时不出现,不编辑时出现
*/
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
//再次编辑是否清空之前文字
textField.clearsOnBeginEditing = YES;
//默认文字:text
textField.text = @"123456789";
//设置左视图:leftView
//位置无效
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(1000, 800, 40, 40)];
view.backgroundColor = [UIColor orangeColor];
textField.leftView = view;
//设置左视图出现状态
textField.leftViewMode = UITextFieldViewModeAlways;
//设置右视图:rightView
//如果存在右视图,一键删除按钮就不存在
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view1.backgroundColor = [UIColor redColor];
//textField.rightView = view1;
//textField.rightViewMode = UITextFieldViewModeAlways;
/*同一个视图不要放到不同位置*/
//键盘上面的自定义视图
//位置无效 宽无效
//可以在inputView上面再添加子视图
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(120, 7880, 320, 40)];
inputView.backgroundColor = [UIColor cyanColor];
//textField.inputAccessoryView = inputView;
//自定义键盘:inputView
UIView *input = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
input.backgroundColor = [UIColor purpleColor];
//UIButton
//textField.inputView = input;
/********!!!!!delegate!!!!!!*******/
textField.delegate = self;
//让输入框开始响应:光标出现,键盘出现
[textField becomeFirstResponder];
[self.view addSubview:textField];
}
#pragma mark - UITextFieldDelegate
//return键的协议方法 *****
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//一般都在这里收回键盘
//让输入框失去第一响应:收回键盘、光标消失
[textField resignFirstResponder];
return YES;
}
//一键删除对应的协议方法 ***
- (BOOL)textFieldShouldClear:(UITextField *)textField{
//默认是YES
//NO:一键删除无效
if (textField.text.length == 3) {
return NO;
}
return YES;
}
//输入框是否可以开始编辑 ***
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//YES:可以编辑 NO:不可以编辑
return YES;
}
//输入框是否可以结束编辑 ***
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//YES:可以结束编辑 NO:不可以结束编辑
// if (textField.text.length < 11) {
// return NO;
// }
return YES;
}
//当输入框文字出现变化就响应的方法 **
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//range : location length
NSLog(@"%lu %lu",(unsigned long)range.length, (unsigned long)range.location);
NSLog(@"%@",string);
return YES;
}
//输入框开始响应就调用的方法 ****
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"begin");
}
//输入框结束响应就调用的方法 *****
- (void)textFieldDidEndEditing:(UITextField *)textField{
//获取输入框里面的文字内容:text
NSLog(@"%@",textField.text);
NSLog(@"end");
}
@end
UITextView
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//关闭自动布局
self.automaticallyAdjustsScrollViewInsets = NO;
/*******UITextView*********/
//UITextView->UIScrollView->UIView
//无:提示文字、密文、边框、一键删除、自适应文字大小、左右视图
//实例化
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 220, 100)];
//背景颜色
textView.backgroundColor = [UIColor grayColor];
//更改字体颜色
textView.textColor = [UIColor redColor];
//更改字体大小
textView.font = [UIFont systemFontOfSize:22.0];
//更改对齐方式
textView.textAlignment = NSTextAlignmentLeft;
//更改键盘颜色
textView.keyboardAppearance = UIKeyboardAppearanceDark;
//更改键盘类型
textView.keyboardType = UIKeyboardTypeEmailAddress;
//自动大写
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//自动纠错
textView.autocorrectionType = UITextAutocorrectionTypeYes;
//return键样式
textView.returnKeyType = UIReturnKeyDone;
//默认文字
textView.text = @"";
//键盘上面的自定义视图
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
view.backgroundColor = [UIColor orangeColor];
textView.inputAccessoryView = view;
//自定义键盘 inputView
//是否允许滑动
textView.scrollEnabled = YES;
//隐藏垂直滑块
textView.showsVerticalScrollIndicator = NO;
//是否可以编辑
textView.editable = YES;
//突出文字 高亮
textView.dataDetectorTypes = UIDataDetectorTypeAll;
/*******!!!delegate!!!!******/
textView.delegate = self;
//添加
[self.view addSubview:textView];
//placeholder的功能
_placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 210, 40)];
_placeholderLabel.text = @"请输入文字";
_placeholderLabel.textColor = [UIColor lightGrayColor];
_placeholderLabel.font = [UIFont systemFontOfSize:22.0];
[textView addSubview:_placeholderLabel];
if (textView.text.length != 0) {
_placeholderLabel.hidden = YES;
}else{
_placeholderLabel.hidden = NO;
}
}
#pragma mark - UITextViewDelegate
//是否可以开始输入文字
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
}
//是否可以结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
return YES;
}
//网址是否可以连接
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
return YES;
}
//文字出现改变就执行:文字能否更改
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//点击return收回键盘
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
//- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange{
//
//}
//输入框开始响应就执行
- (void)textViewDidBeginEditing:(UITextView *)textView{
}
//输入框失去响应就执行
- (void)textViewDidEndEditing:(UITextView *)textView{
}
//文字出现改变就执行
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length != 0) {
_placeholderLabel.hidden = YES;
}else{
_placeholderLabel.hidden = NO;
}
}
//更改文字
//- (void)textViewDidChangeSelection:(UITextView *)textView{
// textView.text = @"123yu";
//}
@end
网友评论