您的位置:首页 > 产品设计 > UI/UE

文本框 UITextView

2015-09-28 20:01 375 查看

UITextView基本属性

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
[self.view addSubview:textView];

//UITextView是UIScrollView的子类,滚动视图如果检测到当前视图控制器在导航上,会自动留出导航栏的高度
//设置为NO就不会在textView内部留出导航栏的高度
self.automaticallyAdjustsScrollViewInsets = NO;

//设置字体和颜色
textView.font = [UIFont systemFontOfSize:13];
textView.textColor = [UIColor redColor];

//设置是否可以滚动
textView.scrollEnabled = YES;
//设置能够编辑 如果只是现实文本就可设置为NO
textView.editable = YES;
//设置代理
textView.delegate = self;
//背景颜色
textView.backgroundColor = [UIColor whiteColor];

textView.text = @"https://www.baidu.com";
//文本的对其方式
textView.textAlignment = NSTextAlignmentLeft;
/*
NSTextAlignmentLeft
NSTextAlignmentCenter
NSTextAlignmentRight
*/

//文本的选择范围
NSLog(@"%@",NSStringFromRange(textView.selectedRange));

//滚动条位置控制 当UITextView中的文本不能在一个画面全显示的情况下,将会出现滚动条
//设置后滚动条将滚动到 第一个位置
[textView scrollRangeToVisible:NSMakeRange(0, 1)];

//URL 地址 电话号码的链接显示 默认此项功能是OFF状态
//这里需要注意的是 要将文本框设置为不可编辑的状态
textView.dataDetectorTypes = UIDataDetectorTypeAll;


UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"编辑开始前被调用,返回NO,编辑将不会开始");
return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
NSLog(@"编辑结束前被调用,返回NO,编辑将不会开始");
return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"改变文本前被调用");
NSLog(@"%@",NSStringFromRange(range));
//range 是设置变化范围
//text是替代的字符串
return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"编辑开始后被调用");
//当UITextView变成第一响应者时被调用
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"编辑结束后被调用");
//当UITextView失去第一响应者状态时被调用
}

- (void)textViewDidChange:(UITextView *)textView
{
NSLog(@"文本变更时被调用");
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSLog(@"游标移动,选择范围发生变化时被调用");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: