Swift---TextView用法

1、TextView放在一个NSScrollView中,作为documentView存在。

@IBOutlet weak var txtScrollView: NSScrollView!

2、在TextView中写入字符串

self.txtScrollView.documentView?.setString("写入第一句。")

3、在TextView中添加字符串

self.txtScrollView.documentView?.textStorage??.mutableString.appendString("追加第二句。")

4、使滚动条自动滑到最下面

if let height=self.txtScrollView.documentView?.bounds.size.height{
   var y=height-self.txtScrollView.documentVisibleRect.height
   if y<0{
      y=0
   }
   self.txtScrollView.contentView.scrollPoint(NSMakePoint(0, y))

}