Opening HTML source code in Cocoa
I'm trying to display HTML source code in my NSDocument based application. However, it renders the page as Safari would show it.
Here's the code that I use to open HTML:
NSData*data;
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSHTMLTextDocumentType
forKey:NSDocumentTypeDocumentOption];
data = [NSData dataWithContentsOfFile:[self fileName]];
mString = [[NSAttributedString alloc]
initWithData:data options:dict documentAttributes:NULL
er开发者_如何学Cror:outError];
What am I doing wrong?
The correct solution is a mix of your original code and the bogus solution I gave you in my previous answer (which I've deleted). Use NSPlainTextDocumentType
as the type, as you were doing originally, but use initWithData:options:documentAttributes:error:
, not initWithHTML:options:documentAttributes:
.
Alternatively, create a plain NSString holding the source code, and then create an attributed string with that plain string plus whatever attributes you want to apply to the whole document (e.g., fixed-pitch font).
精彩评论