版权声明:文章为本人个人总结,如需转载请声明 https://blog.csdn.net/weixin_39624536/article/details/84193053
最近使用UITextField 和 UITextView 。对于其相关内容进行了一些总结,主要方便自己以后的使用
UITextView:
能输入多行,可以滚动显示浏览全文,不可以设置提醒文字(没有placeholder属性),无占位,继承自UIScrollView。
UITextView的常规方法
//初始化UITextView
UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 300, 44)];
//设置背景颜色
textview.backgroundColor=[UIColor blueColor];
//设置当文字超过视图的边框时是否允许滑动,默认为“YES”
textview.scrollEnabled = NO;
//设置是否允许编辑内容,默认为“YES”
textview.editable = YES;
//设置代理方法的实现类
textview.delegate = self;
//设置字体名字和字体大小;
textview.font=[UIFont fontWithName:@"Arial" size:18.0];
// 设置显示文字颜色
textview.textColor = [UIColor blackColor];
//设置自动大写的方式
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 自动大写方式有以下几种:
enum {
UITextAutocapitalizationTypeNone, //不自动大写
UITextAutocapitalizationTypeWords, //单词首字母大写
UITextAutocapitalizationTypeSentences, //句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, //所有字母都大写
} UITextAutocapitalizationType;
//设置键盘类型一般为默认
textview.keyboardType = UIKeyboardTypeDefault;
typedef enum {
UIKeyboardTypeDefault, //默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符
UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, //数字键盘
UIKeyboardTypePhonePad, //电话键盘
UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点
UIKeyboardTypeTwitter, // 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
//设置return键的类型
textview.returnKeyType = UIReturnKeyDefault;
typedef enum {
UIReturnKeyDefault, //默认 灰色按钮,标有Return
UIReturnKeyGo, //标有Go的蓝色按钮
UIReturnKeyGoogle, //标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin, //标有Join的蓝色按钮
UIReturnKeyNext, //标有Next的蓝色按钮
UIReturnKeyRoute, //标有Route的蓝色按钮
UIReturnKeySearch, //标有Search的蓝色按钮
UIReturnKeySend, //标有Send的蓝色按钮
UIReturnKeyYahoo, //标有Yahoo的蓝色按钮
UIReturnKeyDone, //标有Done的蓝色按钮
UIReturnKeyEmergencyCall, //紧急呼叫按钮
} UIReturnKeyType;
//自适应高度
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone //不自动调整。
UIViewAutoresizingFlexibleLeftMargin //自动调整view与父视图左边距,以保证右边距不变
UIViewAutoresizingFlexibleWidth //自动调整view的宽度,保证左边距和右边距不变
UIViewAutoresizingFlexibleRightMargin //自动调整view与父视图右边距,以保证左边距不变
UIViewAutoresizingFlexibleTopMargin //自动调整view与父视图上边距,以保证下边距不变
UIViewAutoresizingFlexibleHeight //自动调整view的高度,以保证上边距和下边距不变
UIViewAutoresizingFlexibleBottomMargin //自动调整view与父视图下边距,以保证上边距不变
};
//文本显示的位置默认为居左
textview.textAlignment = NSTextAlignmentLeft;
typedef NS_ENUM(NSInteger, NSTextAlignmentExpand) {
NSTextAlignmentTop = 5, // Visually top aligned
NSTextAlignmentBottom = 6, // Visually bottom aligned
NSTextAlignmentLeftTop = 7, // Visually left and top aligned
NSTextAlignmentRightTop = 8, // Visually right and top aligned
NSTextAlignmentLeftBottom = 9, // Visually left and bottom aligned
NSTextAlignmentRightBottom = 10, // Visually right and bottom aligned
} NS_ENUM_AVAILABLE_IOS(6_0);
//显示数据类型的连接模式(如电话号码、网址、地址等)
textview.dataDetectorTypes = UIDataDetectorTypeAll;
//设置显示的文本内容
textview.text = @"UITextView详解";
UITextView代理方法
//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return YES;
}
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView
{
//使用了textView.text做placeholder,所以开始编辑前要变换输入框内部的文本颜色,并计算文本长度
textView.textColor = UIColorFromRGB(0x333333);
[self calculateTextViewLength:textView];
}
//将要结束编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
return YES;
}
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView
{
}
//内容发生改变编辑 自定义文本框placeholder
//有时候我们要控件自适应输入的文本的内容的高度,只要在textViewDidChange的代理方法中加入调整控件大小的代理即可
- (void)textViewDidChange:(UITextView *)textView
{
//这里计算已经输入文案的长度
[self calculateTextViewLength:textView];
}
//内容将要发生改变编辑 限制输入文本长度 监听TextView 点击了ReturnKey 按钮
//@param textView textView
//@param range 范围
//@param text text
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//回车按钮收起键盘
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
//限制输入内容的长度
if (range.location >= 200) {
return NO;
}
return YES;
}
//自定义方法
//计算已经输入的字符长度:self.inputNumLabel.text
- (void)calculateTextViewLength:(UITextView*)textView
{
NSInteger length = textView.text.length;
self.inputNumLabel.text = [NSString stringWithFormat:@"%d/200",length];
}
UITextView的Placeholder实现方法
由于UITextView继承与UIController,所以不像UITextField可以直接设置placeholder,只能自己实现。
1、使用UITextView.text 做placeholder
1、把UITextView的text属性当成“placeholder”使用。
2、在开始编辑的代理方法里清除“placeholder”。
3、在结束编辑的代理方法里根据条件设置“placeholder”。
这个方法可以满足某些需求,但是确实显示并不是太方便
2、自己重写TextView(继承UITextView,加入新的placeholder)
如果在工程里经常使用,可以对系统的UITextView进行封装
3、给TextView添加一个UILabel的子控件
这个方法,嗯,想法是可以的
4、UITextView内部有一个名为“_placeHolderLabel”的私有成员变量,通多Runtime可以访问
这个方法应该是最简单方便,效果还好的,值得学习!
注:关于placeHolder,我觉得下面这篇文章总结的很好
本文还参考了其他的文章,不在这里一一标出,表示感谢