我正在将一个简单的HTML加载到NSTextView中。我已经为NSTextView设置了字体属性,但是HTML的加载忽略了这些字体,默认值始终为'Times-Roman 12 size'。目前我正在枚举属性并设置字体大小。但为了改变尺寸,这样做似乎很昂贵。这是唯一的方法吗?有没有更好的办法?在将HTML加载到NSTextView时设置默认字体和大小
HTML:
This is just a title
加载HTML为字号的NSTextView &后续变化:
NSData *valueInDataFormat = [bodyContent dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *textToBeInserted = [[NSAttributedString alloc] initWithHTML:valueInDataFormat documentAttributes:nil];
NSDictionary *rtfTextAttributes = [self defaultTextAttributesFor:RTFText];
//inserttext mimic what user do; so it takes the typingattributes
//ref: http://www.cocoabuilder.com/archive/cocoa/113938-setting-default-font-of-an-nstextview.html#114071
[entryContent setString:@""];
[entryContent setTypingAttributes:rtfTextAttributes];
[entryContent insertText:textToBeInserted];
//now change font size
NSTextStorage *content = [entryContent textStorage];
[content beginEditing];
NSRange totalRange = NSMakeRange (0, content.length);
[content enumerateAttributesInRange: totalRange
options: 0
usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) {
NSLog (@"range: %@ attributes: %@",
NSStringFromRange(range), attributes);
NSFont *font = [attributes objectForKey:NSFontAttributeName];
if (font){
[content removeAttribute:NSFontAttributeName range:range];
font = [[NSFontManager sharedFontManager] convertFont:font toSize:[font pointSize] + 3];
[content addAttribute:NSFontAttributeName value:font range:range];
}
}];
[content endEditing];
[entryContent didChangeText];
2012-10-29
jjude