gpt4 book ai didi

html - ios - 在保留 HTML 属性的同时编辑 NSMutableAttributedString

转载 作者:可可西里 更新时间:2023-11-01 06:15:10 25 4
gpt4 key购买 nike

我有一个 HTML 格式的文本,我想在我的 iOS 应用程序中使用这个文本。我正在使用 NSAttributedString通过 HTML 标签(例如 <strong></strong>)维护文本的属性。问题是,[NSAttributedString initWithData:options:documentAttributes:error:]将默认字体转换为 Times New Roman 的文本。我尝试使用 NSMutableAttributedString将字体更改为 [UIFont systemFontOfSize:] , 但它破坏了它通过 HTML 标签拥有的属性。我该如何解决这个问题?有没有办法更改默认字体?

编辑:例如,我要解析的文本如下:

"Activities are typically <strong>directed at a group of people</strong> rather than individuals"

下面的代码将其解析为带有 Times New Roman 的 HTML 文本:

NSMutableAttributedString* itemWithStyle = [[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) documentAttributes:nil error:nil];

输出:

Activities are typically directed at a group of people rather than individuals

打印时的样子:

Activities are typically {
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0xf1b34e0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSStrokeWidth = 0;
}directed at a group of people{
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x9a8aa10> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSStrokeWidth = 0;
} rather than individuals{
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0xf1b34e0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSStrokeWidth = 0;
}

当我添加以下代码时,文本失去了它的属性。

[itemWithStyle addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]} range:NSMakeRange(0, itemWithStyle.length)];

最佳答案

我遇到了同样的问题并解决了这个问题:

NSMutableString * content = [[NSMutableString alloc] 
initWithString:@"<div style='font-size:15px; font-family: HelveticaNeue'>"];
[content appendString:self.article.content];
[content appendString:@"</div>"];

不确定是否有“更好”的方法,但这对我有用

关于html - ios - 在保留 HTML 属性的同时编辑 NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23428291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com