Set default font and size when loading HTML in NSTextView

I am loading plain HTML in an NSTextView. I have already set the font attributes for NSTextView, but HTML loading ignores these fonts and is always "Times-Roman 12 size" by default. I am currently listing attributes and setting the font size. But it seems expensive for this just to resize. Is this the only way to do this? Is there a better way?

HTML:

<p>This is just a <b>title</b></p> 

Loading HTML into an NSTextView and then changing fontsize:

  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]; 
+4
source share
2 answers

More recently, there was a problem, in the end I found that you can use the <style> block at the beginning of the HTML content and set font-family to the system default. This works well and allows HTML to describe all bold and italic attributes when using a system font.

 NSString* bodyContent = @"<p>This is just a <b>title</b></p>"; NSMutableString *htmlContent = [NSMutableString string]; [html appendString: @"<style>body { font-family: "]; [html appendString: [[NSFont systemFontOfSize: 12] fontName]]; [html appendString: @"; }</style>"]; [html appendString: bodyContent]; NSData *htmlData = [htmlContent dataUsingEncoding:NSUTF8StringEncoding]; NSAttributedString *html = [[NSAttributedString alloc] initWithHTML: htmlDescriptionData baseURL: NULL documentAttributes: NULL]; 

There might be a more elegant solution, but I haven't found it yet.

+3
source

You can use the WebPreferences object to control the font used to create the attribute string from HTML.

The -initWithHTML:options:documentAttributes: method -initWithHTML:options:documentAttributes: dictionary. One of the recognized keys for this dictionary is NSWebPreferencesDocumentOption . The value is an instance of WebPreferences that will be used when interpreting HTML.

A WebPreferences instance has several font family settings. One that is used when no other font attributes are specified in the HTML is -standardFontFamily . If HTML defines a font class (for example, "sans-serif") but does not have a specific font, then the settings for that font class are used (for example, -sansSerifFontFamily ).

WebPreferences also has font size settings.

For instance:

 WebPreferences *webPreferences = [[WebPreferences alloc] initWithIdentifier:@"com.company.app.something"]; webPreferences.standardFontFamily = [someFont familyName]; // or a hard-coded family name like @"Helvetica Neue" webPreferences.defaultFontSize = [NSFont systemFontSize]; ... [[NSAttributedString alloc] initWithHTML:htmlData options:@{NSWebPreferencesDocumentOption : webPreferences} documentAttributes:NULL]; 

There is a caveat: the system font has a family that is hidden from WebPreferences / NSAttributedString . On the Mavericks, his name is ".Lucida Grande UI"; on Yosemite, its name is ". Helvetica Neue UI." For some reason, they cannot be used when converting HTML to an attribute string using the WebPreferences object. My best guess is because they are not listed in [[NSFontManager sharedFontManager] availableFontFamilies] . When you try to use such a font family, the system uses Times (yuck!) By default.

+2
source

Source: https://habr.com/ru/post/1442814/


All Articles