Re: Bad solution for RTF template?
Re: Bad solution for RTF template?
- Subject: Re: Bad solution for RTF template?
- From: Boaz Stuller <email@hidden>
- Date: Mon, 2 Jun 2003 22:47:28 -0400
This is actually quite easy; you just call NSMutableAttributedString's
-mutableString method, make whatever changes you want to the returned
NSMutableString and your attributed string will be automagically
updated to match, including the fixing up of attributes .
Bo
PS. Here's a quick example I posted elsewhere:
NSMutableAttributedString* myAttrString = [[NSMutableAttributedString
alloc] initWithString:@"I program in Cocoa."];
[myAttrString addAttribute:@"testAttr" value:@"testValue"
range:NSMakeRange(2,7)];
NSMutableString* myString = [myAttrString mutableString];
[myString replaceOccurrencesOfString:@" " withString:@"++" options:0
range:NSMakeRange(0, [myString length])];
NSLog(@"%@", myAttrString);
This prints out "I++{}program{testAttr = testValue; }++in++Cocoa.{}"
Notice how it even moves the attribute over so it's still set on the
word "program".
On Monday, June 2, 2003, at 09:53 PM, Ian Gillespie wrote:
Hi Carl,
I am currently trying to solve the same problem, but without much
luck. I hadn't thought of writing it to a temporary file.
Interesting idea, not very clean, but at least it works.
~Ian
The problem I'm having is in doing this in a sane way -
NSMutableAttributedString doesn't have a
replaceOccurrencesOfString:withString:options:range: method like
NSMutableString does, and I can't find a way to replace characters in
the string and maintain overall text attributes (bold in the right
spots, font size, etc).
So the current solution I have is to read the RTF file into an
NSMutableString, run through it doing all of my string substitutions,
and write it to a temporary file (in /tmp). Then I use NSTextView's
readRTFDFromFile: to get the string back in RTF format with all of the
attributes correct.
I'd like to have a better solution - anyone have any suggestions? I'd
be happy with just being able to rid myself of the temporary file
solution, but I'd rather there was a good way to go about it.
I'm sure this is one of those times when I've just blatantly missed
the
appropriate tidbit in the documentation. The way I'm doing it is the
hard way, and if there's one thing that Cocoa has taught me it's that
it's not hard.
Thanks folks!
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >(no subject) (From: Ian Gillespie <email@hidden>) |