Swift UITextView(多行文本控件)
来源:互联网 发布:阿里云备案拿手机拍照 编辑:程序博客网 时间:2024/04/30 11:57
一、创建
UITextView优势:弥补textfield单行输入的缺点,弥补lable显示固定行数的缺点。
let textview = UITextView(frame: CGRectMake(30, 80, 250, 100))textview.layer.borderWidth = 0.4textview.layer.cornerRadius = 4textview.layer.borderColor = UIColor.redColor().CGColortextview.layer.masksToBounds = truetextview.becomeFirstResponder()self.view.addSubview(textview)
二、常用的控件API
2.1 是否可以被编辑,返回false,就是readOnly
textview.editable = true
2.2 显示文本
textview.text = "Hello Swift and Object-C "
2.3 当文本是电话或者是一个网址,那么高亮显示,点击并跳转或者拨打电话
PhoneNumber : 电话号码链接 Link : 网址的链接 All : 电话号码+网址链接 None : 都不创建链接
textview.dataDetectorTypes = .All
2.4 光标和选择范围
print(textview.selectedRange)
2.5 对选定文字进行加粗、下划线等操作
textview.allowsEditingTextAttributes = true
2.6 避免用户对选定一定范围内文本进行copy
指定光标一定到指定位置,如果设置selectable为false的话,文字不能被选择,防止用户copy
textview.scrollRangeToVisible(NSMakeRange(0, 10))textview.selectable = false
2.7 常用的一些代理方法
textview.delegate = self
2.8 编辑开始被调用,返回false,编辑不会被开始
func textViewShouldBeginEditing(textView: UITextView) -> Bool { return true }
2.9 编辑结束前调用,返回false,编辑不会结束,光标一直存在
func textViewShouldEndEditing(textView: UITextView) -> Bool { return true }
2.10 改变文本前被调用,向range中设置变化范围,text中设置变化后的字符串,返回false,变化将不会反应
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { print(textView.text) return true }
2.11 编辑开始后被调用,获取焦点被调用
func textViewDidBeginEditing(textView: UITextView) { print(textView.text) }
2.12 编辑结束后调用
func textViewDidEndEditing(textView: UITextView) { print(textView.text) }
2.13 文本变更时被调用(结果是每输入一个字符时都会被调用)
func textViewDidChange(textView: UITextView) { print(textView.text) }
2.14 游标移动,选择范围发生变化时被调用
func textViewDidChangeSelection(textView: UITextView) { print(textView.text) }
注意: UITextview并没有像UITextField控件有textFieldShouldReturn监听点击return回车键,因为在多行文本控件中,点击return键被意味着是正常的换行行为。因为,我们需要在界面中自己增加一个完成按钮,点击按钮后调用UITextview的resgisterFirstResponde方法,从而收起键盘。
0 0
- Swift UITextView(多行文本控件)
- Swift - 多行文本输入框(UITextView)的用法
- MRC下多行文本输入UITextView
- UITextView多行文本输入框
- 用户输入文本框控件(UITextField)与多行文本控件(UITextView)
- IOS中UITextView(多行文本框)控件的简单用法
- 带提示(hint)的多行文本输入--UITextView
- 单行文本框和多行文本框UiTextfield UiTextView
- iOS开发 UITextView(多行文本框)控件的一些用法(显示文字 退出键盘等)
- swift UI专项训练19 TextView 多行文本
- UITextView 多行文本的输入和显示 的使用方法详细
- iOS--UITextView 多行文本的输入和显示 的使用方法详细
- IOS--UITextView 多行文本的输入和显示 的使用方法详细
- .net cf 多行文本的Button控件
- IOS中的UISegmentedControl控件实现多行文本
- 打印多行文本
- 多行文本绘制
- 多行文本框
- iOS textview滚动到底部
- ListView下拉刷新模板方式
- 将图片设置为主界面背景
- Android学习之自定义基类BaseApplication和BaseActivity
- Logback.xml文件配置详解
- Swift UITextView(多行文本控件)
- 创意户外金属便携打火机
- 云计算环境下的大规模图数据处理技术
- 工程打包-另辟蹊径 iOS
- RunLoop运行循环
- 为什么要写技术blog?
- Reducing Compilation Time: Unity Builds
- ubuntu下 minicom不能打印信息的解决
- 服务器的瞬时diffie-hellman 公共密钥过弱 jetty解决办法