【发布时间】:2017-09-20 21:15:45
【问题描述】:
您好,我是 Objective C 的新手。我正在将 htmlString 转换为 NSMutableAttributedString。但是当我更改 NSMutableAttributedString 的字体大小时,它会从我的文本中删除粗体字体,请提供任何解决方案。
我的代码:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil ];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
// overView.font = [UIFont systemFontOfSize:16];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, attributedString.length)];
}
else{
// overView.font = [UIFont systemFontOfSize:12];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, attributedString.length)];
}
overView.attributedText = (NSAttributedString *)attributedString;
任何帮助将不胜感激。
【问题讨论】:
-
不是它,你知道。还有一个
boldSystemFontOfSize方法。 -
@vadian 它会将所有字符串加粗。
-
当然,因为范围是为整个字符串指定的。如果你想要一个部分字符串,你必须调整范围。
-
实际上我是从 API 获取 htmlString 的,它有一些格式,比如有些行被 包围,有些是斜体。我只是将它转换为 NSMutableAttributedString。之后我想根据 iPad 或 iPhone 更改字符串的字体大小。 @vadian
标签: ios objective-c nsmutableattributedstring