html默认文字厚度,cocoa – 在将HTML加载到NSTextView时设置默认字体和大小

我正在将一个简单的

HTML加载到NSTextView中.我已经为NSTextView设置了字体属性,但HTML的加载忽略了这些字体,默认情况下总是为’Times-Roman 12 size’.目前我正在枚举属性并设置字体大小.但这样做只是为了改变尺寸似乎很昂贵.这是唯一的方法吗?有没有更好的办法?

HTML:

This is just a title

将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];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值