Pripyat
Pripyat

Reputation: 2937

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
               error:outError];

What am I doing wrong?

Upvotes: 1

Views: 275

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96323

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).

Upvotes: 1

Related Questions