0

This is probably easy, but I can not seam to figure it out - maybe it's late. I have a simple program that takes the text from an NSTextView and saves it as rtf. Saving the text itself works great, I just can not figure out how to get the attributes to tag along.

Code:

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

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

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

I know I need an NSDictionary for the documentAttributes, so how do I get that from the view?

What am I missing?

1
  • There are no attributes given you don't set any in the attributed string (not even sure what that string method is that you're calling).
    – Droppy
    Oct 18, 2014 at 9:30

1 Answer 1

0

It seems that you are asking the textView for its string property. You need to ask it for its attributedString property:

NSAttributedString *saveString = textView.attributedString;

You can get the attributes from an attributed string like this:

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];

I have used this method to get attributes many times however I have never saved to RTF so I don't know exactly how this will turn out. All the attributes will be in the dictionary however.

1
  • Thanks, that's what I needed. Only change to your code for it to work is on the NSData line. NSData *writeResults = [saveString RTFFromRange:NSMakeRange(0, saveString.length) documentAttributes:allAttributes]; Oct 19, 2014 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.