iOS - UITextView
前言
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextInput> @available(iOS 2.0, *) public class UITextView : UIScrollView, UITextInput
- UITextView 具有 label 大部分属性,以及 textField 的属性。
1、UITextView 的创建
-
Objective-C
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, self.view.frame.size.width - 20, 400)]; // 将 textView 添加到 view [self.view addSubview:textView];
-
Swift
let textView:UITextView = UITextView(frame: CGRectMake(10, 30, self.view.frame.size.width - 20, 400)) // 将 textView 添加到 view self.view.addSubview(textView)
2、UITextView 的设置
-
Objective-C
// 设置背景颜色 textView.backgroundColor = [UIColor lightGrayColor]; // 设置文本 /* 由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑 */ textView.text = @"写代码快乐\n15911110524\n写代码快乐\nhttps://www.baidu.com\n写代码快乐"; // 设置对齐方式 /* NSTextAlignmentLeft = 0, // Visually left aligned NSTextAlignmentCenter = 1, // Visually centered NSTextAlignmentRight = 2, // Visually right aligned NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned. NSTextAlignmentNatural = 4, // Indicates the default alignment for script */ textView.textAlignment = NSTextAlignmentLeft; // 设置文本颜色 textView.textColor = [UIColor redColor]; // 设置字体 textView.font = [UIFont boldSystemFontOfSize:20]; // 对文本中的电话和网址自动加链接 /* UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection UIDataDetectorTypeLink = 1 << 1, // URL detection UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // Event detection UIDataDetectorTypeNone = 0, // No detection at all UIDataDetectorTypeAll = NSUIntegerMax // All types */ textView.dataDetectorTypes = UIDataDetectorTypeAll; // 禁止编辑 /* 设置为只读,不再能输入内容 */ textView.editable = NO; // 禁止选择 /* 禁止选中文本,此时文本也禁止编辑 */ textView.selectable = NO; // 禁止滚动 textView.scrollEnabled = NO; // 获取当前选择的范围 NSRange range = textView.selectedRange; // 设置可以对选中的文字加粗 /* 选中文字时可以对选中的文字加粗 */ textView.allowsEditingTextAttributes = YES; // 设置 textView 的代理,需遵守协议 <UITextViewDelegate> textView.delegate = self;
-
Swift
// 设置背景颜色 textView.backgroundColor = UIColor.lightGrayColor() // 设置文本 /* 由于 UITextView 具有滚动功能,当 text 文本内容较多时可以实现滚动阅读和编辑 */ textView.text = "写代码快乐\n15911110524\n写代码快乐\nhttps://www.baidu.com\n写代码快乐" // 设置文本颜色 textView.textColor = UIColor.redColor() // 设置对齐方式 /* case Left // Visually left aligned case Center // Visually centered case Right // Visually right aligned case Justified // Fully-justified. The last line in a paragraph is natural-aligned. case Natural // Indicates the default alignment for script */ textView.textAlignment = .Left // 设置字体 textView.font = UIFont.systemFontOfSize(20) // 对文本中的电话和网址自动加链接 /* PhoneNumber // Phone number detection Link // URL detection Address // Street address detection CalendarEvent // Event detection None // No detection at all All // All types */ textView.dataDetectorTypes = .All // 禁止编辑 /* 设置为只读,不再能输入内容 */ textView.editable = false // 禁止选择 /* 禁止选中文本,此时文本也禁止编辑 */ textView.selectable = false // 禁止滚动 textView.scrollEnabled = false // 获取当前选择的范围 let range:NSRange = textView.selectedRange // 设置可以对选中的文字加粗 /* 选中文字时可以对选中的文字加粗 */ textView.allowsEditingTextAttributes = true // 设置 textView 的代理,需遵守协议 UITextViewDelegate textView.delegate = self
3、UITextViewDelegate 的协议方法
需遵守协议 UITextViewDelegate,并设置代理
-
Objective-C
// 将要开始编辑,编辑开始前被调用 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { return YES; } // 已经开始编辑,编辑开始后被调用 - (void)textViewDidBeginEditing:(UITextView *)textView { } // 将要结束编辑,编辑结束前被调用 - (BOOL)textViewShouldEndEditing:(UITextView *)textView { return YES; } // 已经结束编辑,编辑结束后被调用 - (void)textViewDidEndEditing:(UITextView *)textView { NSLog(@"编辑的内容是 : %@", textView.text); } // 文本修改,文本修改前被调用 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; } // 文本变更,文本变更时被调用,每输入一个字符时都会被调用 - (void)textViewDidChange:(UITextView *)textView { } // 游标移动,选择范围发生变化时被调用 - (void)textViewDidChangeSelection:(UITextView *)textView { }
-
Swift
// 将要开始编辑,编辑开始前被调用 func textViewShouldBeginEditing(textView: UITextView) -> Bool { return true } // 已经开始编辑,编辑开始后被调用 func textViewDidBeginEditing(textView: UITextView) { } // 将要结束编辑,编辑结束前被调用 func textViewShouldEndEditing(textView: UITextView) -> Bool { return true } // 已经结束编辑,编辑结束后被调用 func textViewDidEndEditing(textView: UITextView) { print("编辑的内容是 : \(textView.text)") } // 文本修改,文本修改前被调用 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { return true } // 文本变更,文本变更时被调用,每输入一个字符时都会被调用 func textViewDidChange(textView: UITextView) { } // 游标移动,选择范围发生变化时被调用 func textViewDidChangeSelection(textView: UITextView) { }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Can't create handler inside thread that has not called Looper.prepare(...
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/52163029 最近做项目时出现个问题。 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override public void handleMessage(android.os.Message msg) { super.handleMessage(msg); Bundle bundle = msg.getData(); isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());//isStop为基类中的一个私有成员 }; }; 但不知道为啥一直报错:Can't create handler inside thread that has not called Looper.prepare()。 搜索后发现,原因是此Handler没...
- 下一篇
Android ListView子item高度定长固定值无效问题
在项目开发中遇到一个实际问题:ListView中,打算给每个子item设定一个具体的高度值如128dp,256dp,在子item中把根布局的高度值写死写成定长,但是不管怎么样,高度一直都没变化。开始以为是ListView的问题,后来又换成RecyclerView,还是老样子,莫名其妙的问题!后来查了查,才知道,如果在ListView或者RecyclerView这样的列表中设定写死子item的高度值,比如: android:layout_height="128dp" 如果使得这样的设定生效,那么最关键的需要在这个ListView的子item的根布局的高度后,再增加一个属性设置,联合设置最小值: android:minHeight="128dp" 这样在ListView显示子item时候,高度才是定长的值128dp。
相关文章
文章评论
共有0条评论来说两句吧...