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
原创粉丝点击