2013-07-16 33 views
8

我試圖將一個屬性字符串粘貼到我的NSTextView,但它只是顯示爲純文本,沒有屬性。我創造了我的字符串,像這樣:NSTextView和NSAttributedString

NSString *str = @"Parsing Directory Structure\n\n"; 

NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:str]; 
NSDictionary *attributes = @{ 
          NSForegroundColorAttributeName : [NSColor blueColor], 
          NSFontAttributeName : [NSFont fontWithName:@"HelveticaNeue-Bold" size:20.f] 
          }; 
[attrstr setAttributes:attributes range:NSRangeFromString(str)]; 

[[self.textView textStorage] appendAttributedString:attrstr]; 

,並在NSTextView(滾動視圖中)我已經得到了「允許富文本」框中選中靜止了,可編輯勾選。我基本上試圖使用這個像控制檯輸出窗口。

回答

5

NSMakeRangeFromString解析範圍的文本表示,它不會創建覆蓋字符串的範圍。由於您的文本不包含整數,因此返回範圍{0, 0} - 位置和長度都爲零。所以雖然你的文字沒有風格。

替換爲NSMakeRange(0, str.length)並且您的代碼應該可以工作。

+0

謝謝!這工作完美。 – Gargoyle