开发者

Opening HTML source code in Cocoa

开发者 https://www.devze.com 2022-12-22 05:20 出处:网络
I\'m trying to display HTML source code in my NSDocument based application. However, it renders the page as Safari would show it.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号