UITextView
介绍
- 可展示和编辑多行文本的控件。有对应的UITextViewDelegate委托协议。
显示效果
//初始化并且设置位置
textView = [[UITextView alloc] initWithFrame:CGRectMake(x,y,w,h)];
//设置字体和字体大小
textView.font = [UIFont fontWithName:@"Arial" size:18.0];
//字体颜色
textView.textColor = [UIColor blackColor];
//背景颜色
textView.backgroundColor = [UIColor whiteColor];
//设置它显示的内容
textView.text = @"内容";
[textView setText:@"内容"];
其他设置
//限制行数(0为不限制)
[textView setNumberOfLines:0];
//是否可以拖动
textView.scrollEnabled = YES;
键盘风格
//UIKeyboardTypeDefault 默认键盘,支持所有字符
//UIKeyboardTypeASCIICapable 支持ASCII的默认键盘
//UIKeyboardTypeNumbersAndPunctuation 标准电话键盘,支持+*#字符
//UIKeyboardTypeURL URL键盘,支持.com按钮 只支持URL字符
//UIKeyboardTypeNumberPad 数字键盘
//UIKeyboardTypePhonePad 电话键盘
//UIKeyboardTypeNamePhonePad 电话键盘,也支持输入人名
//UIKeyboardTypeEmailAddress 用于输入电子 邮件地址的键盘
//UIKeyboardTypeDecimalPad 数字键盘 有数字和小数点
//UIKeyboardTypeTwitter 优化的键盘,方便输入@、#字符
//UIKeyboardTypeAlphabet
textView.keyboardType = UIKeyboardTypeNumberPad;
//键盘外观
//UIKeyboardAppearanceDefault 默认外观,浅灰色
//UIKeyboardAppearanceAlert 深灰 石墨色
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
返回键
//UIReturnKeyDefault 默认 灰色按钮,Return按键
//UIReturnKeyGo Go的蓝色按钮
//UIReturnKeyGoogle Google的蓝色按钮,用语搜索
//UIReturnKeyJoin Join的蓝色按钮
//UIReturnKeyNext Next的蓝色按钮
//UIReturnKeyRoute Route的蓝色按钮
//UIReturnKeySearch Search的蓝色按钮
//UIReturnKeySend Send的蓝色按钮
//UIReturnKeyYahoo Yahoo的蓝色按钮
//UIReturnKeyEmergencyCall 紧急呼叫按钮
textView.returnKeyType =UIReturnKeyDone;
文本位置
//UIControlContentVerticalAlignmentFill 填满
//UIControlContentVerticalAlignmentCenter 居中
//UIControlContentVerticalAlignmentButtom 底部
//UIControlContentVerticalAlignmentTop 顶部
textView.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
位置调整
//UIViewAutoresizingNone 默认
//UIViewAutoresizingFlexibleLeftMargin 左边距
//UIViewAutoresizingFlexibleWidth 宽度
//UIViewAutoresizingFlexibleRightMargin 右边距
//UIViewAutoresizingFlexibleTopMargin 上边距
//UIViewAutoresizingFlexibleHeight 高度
//UIViewAutoresizingFlexibleBottomMargin 底边距
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
字母设置
//UITextAutocapitalizationTypeNone 不自动大写
//UITextAutocapitalizationTypeWords 单词首字母大写
//UITextAutocapitalizationTypeSentences 句子的首字母大写
//UITextAutocapitalizationTypeAllCharacters 所有字母都大写
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
协议代理
//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;
//内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView;
//焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView;
纠错功能
//UITextAutocorrectionTypeDefault 默认(不开启)
//UITextAutocorrectionTypeNo 不自动纠错
//UITextAutocorrectionTypeYes 自动纠错
textView.autocorrectionType = UITextAutocorrectionTypeNo;
- (BOOL)textView:(UITextView*)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
//这对于想要加入撤销选项的应用程序特别有用
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。
//要防止文字被改变可以返回NO
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
return YES;
}
键盘收回
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//按return键收回键盘
//主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
[textFiled resignFirstResponder];
return YES;
}