2015-04-01 139 views
2

我想改变NSTextView的颜色和字体大小,但它似乎并没有工作。这是我的代码。我检查了其他帖子,但他们似乎并没有工作这是我的代码。更改颜色和NStextView的字体

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

回答

4

你应该使用归因字符串:

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

+1

烨说解决了很多problem..thanks .. :) – soldiershin 2015-04-01 17:15:55