HTML:
<p>This is just a <b>Title</b></p>
将HTML加载到NSTextVIEw&随后更改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];解决方法 最近遇到了这个问题,最后我发现可以使用< style>阻止HTML内容的前面,并将Font-family设置为系统默认值.这很好用,并允许HTML在使用系统字体时描述所有粗体和斜体属性.
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];
可能有一个更优雅的解决方案,但我还没有找到它.
总结以上是内存溢出为你收集整理的cocoa – 在将HTML加载到NSTextView时设置默认字体和大小全部内容,希望文章能够帮你解决cocoa – 在将HTML加载到NSTextView时设置默认字体和大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)