avoid initWithURL?
avoid initWithURL?
- Subject: avoid initWithURL?
- From: email@hidden
- Date: Fri, 1 Feb 2002 12:14:26 -0800
I have plain text html in a textView. I can save the text to a file
and use
initWithURL to render the html in another textView. Can I somehow
avoid the
temp file saving and initWithURL and render straight from the textView?
- (id)initWithHTML:(NSData *)data documentAttributes:(NSDictionary
**)dict;
- (id)initWithHTML:(NSData *)data baseURL:(NSURL *)base
documentAttributes:(NSDictionary **)dict;
These are both in NSAtrributedString.h in AppKit (there is a header by
the same name in Foundation, don't let that throw you :->). So convert
your NSString into an NSData. Then pass the NSData in to one of these
methods. The version of the API where you pass in the base URL lets you
have the HTML processed as if it came from a particular URL; you would
use this if you were writing a browser, for instance. If you know the
URL this HTML page should be considered to "live at", supply it there.
A base URL tag in the HTML will be used if it is present, and I believe
will override the one you may pass in (as it should).
As I recall, the dictionary is largely ignored on the way in for HTML;
on the way out it will contain a variety of potentially useful keys,
though.
The only tricky bit here is what encoding to use for the NSData. This
works exactly as it should according to the HTML spec, I believe.
Depending upon the encoding you choose, you may need to include a
CHARSET tag in your HTML. See the HTML spec for more information on how
encodings are handled, if you're not already on top of this.
Good luck!
Ben Haller
Stick Software