2014-10-18 68 views
0

這可能很容易,但我不能縫隙搞清楚 - 可能是晚了。我有一個簡單的程序,將NSTextView中的文本保存爲rtf。保存文本本身效果很好,我只是無法弄清楚如何獲取屬性來標記。屬性沒有與文件保存

代碼:

NSAttributedString *saveString = [[NSAttributedString alloc] 
            initWithString:[textView string]]; 

NSData *writeResults = [saveString 
          RTFFromRange:NSMakeRange:(0, [saveString length]) 
          doumentAttributes:?? ]; 

[writeResults writeToURL:[panel URL] atomically: YES]; 

我知道我需要爲documentAttributesNSDictionary,所以我如何才能從有何看法?

我錯過了什麼?

+0

沒有屬性給你沒有在屬性字符串中設置任何(甚至不知道你調用的是什麼字符串方法)。 – Droppy 2014-10-18 09:30:34

回答

0

看來你問textView的字符串屬性。你要問它的attributedString屬性:

NSAttributedString *saveString = textView.attributedString; 

你可以從一個屬性串的屬性是這樣的:

NSMutableDictionary *allAttributes = [[NSMutableDictionary alloc] init]; 
[saveString enumerateAttribuesInRange:NSMakeRange(0,saveString.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { 
    [allAttrubutes addEntriesFromDictionary:attrs]; 
}]; 
NSData *writeResults = [saveString.string RTFFromRange:NSMakeRange(0,saveString.length) documentAttributes:allAttributes]; 

我用這種方法來獲得屬性很多次,但是我從來沒有保存到RTF,所以我不知道這將如何結果。但是,所有的屬性都將在字典中。

+0

謝謝,這就是我需要的。只有更改爲您的代碼才能使其工作在NSData行上。 NSData * writeResults = [saveString RTFFromRange:NSMakeRange(0,saveString.length)documentAttributes:allAttributes]; – littleDrummerBoy 2014-10-19 02:45:38