//
// AppDelegate.swift
// NSTextView
//
// Created by iDevFans on 16/6/28.
// Copyright © 2016年 http://www.macdev.io. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate,NSTextDelegate, NSTextStorageDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet var textView: NSTextView!
@IBOutlet var attibuteTextView: NSTextView!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// 定义富文本属性
let attributedString = NSMutableAttributedString(string:"attributedString" as String)
attributedString.addAttributes([NSAttributedStringKey.foregroundColor: NSColor.blue as Any ], range: NSMakeRange(0, 10))
attributedString.addAttributes([NSAttributedStringKey.foregroundColor: NSColor.green as Any ], range: NSMakeRange(10, 6))
//设置属性
textView.textStorage?.setAttributedString(attributedString)
textView.textStorage?.delegate = self
}
override func textStorageDidProcessEditing(_ notification: Notification) {
self.perform(#selector(self.setHeightToMatchContents), with: nil, afterDelay: 0.0)
}
///Marker: 设置视图大小
@objc func setHeightToMatchContents() {
let naturalSize: NSSize = self.naturalSize()
if let scrollView = self.textView.enclosingScrollView {
let frame = scrollView.frame
scrollView.frame = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width, naturalSize.height + 4)
}
}
func naturalSize() -> NSSize {
let bounds: NSRect = self.textView.bounds //文本视图大小
let layoutManager: NSLayoutManager = textView.textStorage!.layoutManagers[0] //文本视图布局管理
let textContainer: NSTextContainer = layoutManager.textContainers[0] //获取布局中的文本容器
textContainer.containerSize = NSMakeSize(bounds.size.width, 1.0e7) //设置大小
layoutManager.glyphRange(for: textContainer)
let naturalSize: NSSize? = layoutManager.usedRect(for: textContainer).size
return naturalSize!
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
///Mark: -开始编辑
func textDidBeginEditing(_ notification: Notification) {
if let textView = notification.object as? NSTextView {
let text = textView.string
print("textDidBeginEditing text \(text)")
}
}
///Mark: -结束编辑
func textDidEndEditing(_ notification: Notification) {
if let textView = notification.object as? NSTextView {
let text = textView.string
print("textDidEndEditing text \(text)")
}
}
///Mark: -内容改变
func textDidChange(_ notification: Notification) {
if let textView = notification.object as? NSTextView {
let text = textView.string
print("textDidChange text \(text)")
}
}
}