我正在尝试获取一段以 html 格式设置的文本,以便在 iPhone 的 UITableViewCell 中很好地显示。
到目前为止我有这个:
NSError* error;
NSString* source = @"<strong>Nice</strong> try, Phil";
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithData:[source dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil error:&error];
这种作品。我收到一些以粗体显示“Nice”的文字!但是……它还将字体设置为Times Roman!这不是我想要的字体。我想我需要在 documentAttributes 中设置一些东西,但是,我无法在任何地方找到任何示例。
原文由 phil 发布,翻译遵循 CC BY-SA 4.0 许可协议
弄清楚了。有点熊,也许不是最好的答案。
此代码将经历所有字体更改。我知道它使用“Times New Roman”和“Times New Roman BoldMT”作为字体。但无论如何,这会找到粗体并让我重新设置它们。我也可以在使用时重置尺寸。
老实说,我希望/认为有一种方法可以在解析时进行设置,但如果有的话我找不到。
}