标签:
这篇文章只涉及到基本的使用,日后会写一些关于结合TextKit的备忘
let screenSize = UIScreen.mainScreen().bounds.size let textView = UITextView(frame: CGRectMake(0, 20, screenSize.width, 200)) textView.font = UIFont.systemFontOfSize(20) textView.selectable = false textView.scrollEnabled = true textView.editable = true textView.textColor = UIColor.whiteColor() textView.backgroundColor = UIColor.blackColor() textView.text = "The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document." textView.textAlignment = .Center textView.textContainerInset = UIEdgeInsetsMake(60, 0, 0, 0) textView.keyboardType = .Default textView.returnKeyType = .Default view.addSubview(textView) self.textView = textView
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewDidBeginEdit(_:)), name: UITextViewTextDidBeginEditingNotification, object: textView) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewTextDidChange(_:)), name: UITextViewTextDidChangeNotification, object: textView) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.textViewDidEndEdit(_:)), name: UITextViewTextDidEndEditingNotification, object: textView) func textViewDidBeginEdit(notification: NSNotification) { print(notification.name) } func textViewTextDidChange(notification: NSNotification) { print(notification.object) } func textViewDidEndEdit(notification: NSNotification) { print(notification.name) } deinit{ NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidChangeNotification, object: textView) NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidEndEditingNotification, object: textView) NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidBeginEditingNotification, object: textView) }
extension ViewController: UITextViewDelegate { // 是否应该开始编辑 func textViewShouldBeginEditing(textView: UITextView) -> Bool { print("textViewShouldBeginEditing") return true } // 是否应该停止编辑 func textViewShouldEndEditing(textView: UITextView) -> Bool { print("textViewShouldEndEditing") return true } // 文字视图已经开始编辑 func textViewDidBeginEditing(textView: UITextView) { print("textViewDidBeginEditing") } // 文字视图已经停止编辑 func textViewDidEndEditing(textView: UITextView) { print("textViewDidEndEditing") } // 文字视图是否允许替换文字,每当有文字要被输入或删除都会先调用这个方法 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true } // 文字视图文字已经被替换 func textViewDidChange(textView: UITextView) { print("textViewDidChange") } // 每当有一组文字被选中或删除输入、放大镜的移动,都会调用此方法 func textViewDidChangeSelection(textView: UITextView) { print("textViewDidChangeSelection") }
标签:
原文地址:http://www.cnblogs.com/Alex-sk/p/5595031.html