更改 NStextView 的颜色和字体

change the color and the font of NStextView

我正在尝试更改 NSTextView 的颜色和字体大小,但似乎没有 work.This 是我的 code.I 检查了其他帖子,但它们似乎没有 working.This 是我的代码。

 var area:NSRange = NSMakeRange(0, textView.textStorage!.length)
 self.textView.setTextColor(NSColor.grayColor(), range: area)
 self.textView.textStorage?.font = NSFont(name: "Calibri", size: 16)

您应该使用属性字符串:

let textView = NSTextView(frame: NSMakeRect(0, 0, 100, 100))
let attributes = [NSForegroundColorAttributeName: NSColor.redColor(),
                  NSBackgroundColorAttributeName: NSColor.blackColor()]
let attrStr = NSMutableAttributedString(string: "my string", attributes: attributes)
let area = NSMakeRange(0, attrStr.length)
if let font = NSFont(name: "Helvetica Neue Light", size: 16) {
    attrStr.addAttribute(NSFontAttributeName, value: font, range: area)
    textView.textStorage?.appendAttributedString(attrStr)
}