0
点赞
收藏
分享

微信扫一扫

iOS开发:个人对于textView基础用法的总结(其一)


从事了这么久ios开发,对于textView的使用并不陌生,它和textfield有相似的地方,也有不同的地方,这里只对textView的一些基础用法进行描述,textfield不在这里描述。


一、基础用法:

在.h文件中声明:

@interface ProtocolViewController :UIViewController<UITextViewDelegate>
 {
 UITextView *textView;
 }

 @property (nonatomic,retain)UITextView *textView;

 @end


在.m中初始化:

- (void)init{
 //UITextView(使用须遵守UITextViewDelegate协议)
 self.textView = [UITextView new];  或者 self.textView = [[UITextView alloc] init]; self.textView.frame =  CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 60); 
//设置它的委托方法 
 self.textView.delegate = self;//设置是否可以编辑
 self.textView.editable = YES;

 //设置显示内容
 self.textView.text = @"请输入密码";

 //设置textview里面的字体颜色 
 self.textView.textColor = [UIColor blackColor];//设置文字对齐方式
 self.textView.textAlignment = NSTextAlignmentLeft; //设置字体大小
 self.textView.font = [UIFont systemFontOfSize:25.0];//设置字体名字和字体大小
 self.textView.font = [UIFont fontWithName:@"Arial"size:18.0];//设置边框属性
 self.textView.layer.cornerRadius = 10; //边框弧度
 self.textView.borderColor = [UIColor darkGrayColor].CGColor; //设置边框颜色
 self.textView.layer.borderWidth = 2; //边框宽度 设置是否可以滚动
 //UITextView继承于UIScrollView
 self.textView.scrollEnabled = NO;

 //消除影响(iOS7 如果把UIscrollView 加在导航中一般内容会向下走64)
 self.automaticallyAdjustsScrollViewInsets = NO;//设置背景颜色
 self.textView.backgroundColor = [UIColor whiteColor]; //UITextView下的键盘中return 表示换行
 //返回键的类型
 self.textView.returnKeyType = UIReturnKeyDefault;

 //键盘类型
 self.textView.keyboardType = UIKeyboardTypeDefault;

 //自适应高度 
 self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

 [self.view addSubview: self.textView];//加入到整个页面中
 }

二、协议:

#pragma mark - UITextViewDelegate协议中的方法
 //将要进入编辑模式
 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {return YES;} //已经进入编辑模式
 - (void)textViewDidBeginEditing:(UITextView *)textView {} //将要结束/退出编辑模式
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {return YES;} //已经结束/退出编辑模式
 - (void)textViewDidEndEditing:(UITextView *)textView {} //当textView的内容发生改变的时候调用的方法
 - (void)textViewDidChange:(UITextView *)textView {} //选中textView 或者输入内容的时候调用的方法
 - (void)textViewDidChangeSelection:(UITextView *)textView {} //从键盘上将要输入到textView的时候调用的方法
 //text 将要输入的内容
 //返回YES可以输入内容到textView中,返回NO则不能
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {return YES;}


三、UITextView内容添加之后,让其自动滚到到最后一行(解决UITextView展示文字不展示第一行的问题)

用scrollRangeToVisible函数进行滑动,可以跳动到最后一行内容上面:[self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length, 1)]; 

但是上面的这样设置效果不好。因为重新设置了内容,导致每次都要从顶部跳到最后一行,界面很闪,用户体验非常不好,那么最后的解决方案如下:

self.textView.layoutManager.allowsNonContiguousLayout = NO;

UITextView 中的 layoutManager(NSLayoutManager) 的是否非连续布局属性,系统默认是YES,把它设置为NO后 ,UITextView 就不会再自己重置滑动了。

四、给UITextView添加默认文字(占位文字)

UITextView的初始化和基本设置不在重述,只写关键步骤。

//在UITextView上面覆盖一个UILabel,UILabel设置成全局变量(UILabel的声明、初始化、基本设置也不在描述)
self.label.frame = CGRectMake(5, 8,[UIScreen mainScreen].bounds.size.width-16, 20);  // UILabel的坐标要和UITextView的坐标根据实际情况相结合
self.label.text = @"填充文字"; //设置占位文字
self.label.enabled = NO; //label必须设置为不可用
self.label.backgroundColor = [UIColor clearColor]; //设置背景颜色为透明色 
[self.view addSubview: self.label];//加入到整个页面中

               

实现UITextView的代理方法

- (void)textViewDidChange:(UITextView *)textView {
self.examineText = textView.text;
if(textView.text.length == 0) {  //根据label的长度判断是否展示填充文字
self.label.text = @"填充文字";
}else {
self.label.text = @"";
}
}

举报

相关推荐

0 条评论