soldiershin
soldiershin

Reputation: 1620

change the color and the font of NStextView

I am trying to change the color and the font size of NSTextView but it doesn't seem to work.This is my code.I have checked other posts but they dont seem to be working.This is my code.

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

Upvotes: 3

Views: 2139

Answers (1)

Eric Aya
Eric Aya

Reputation: 70118

You should use attributed strings:

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

Playground

Upvotes: 5

Related Questions