UITextView -- 基础备忘

时间:2021-06-04 04:33:40

UITextView

这篇文章只涉及到基本的使用,日后会写一些关于结合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
  • font:字体
  • selectable:是否可以选中。
  • scrollEnabled:是否可以滚动。
  • editable:是否可以编辑。
  • textColor:文字颜色。
  • backgroundColor:背景色。
  • text:要显示的文字。
  • textAlignment:文字排版样式。
  • textContainerInset:文字的距离textview的内边距。
  • keyboardType:键盘样式。
  • returnKeyType:return键的样式。

监听通知

        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")
}