html - 字体解析问题

标签 html ios objective-c parsing nsattributedstring

如果有人能帮助我解决问题,我将不胜感激。我需要从我得到的 html 字符串中获取一个属性字符串,其中包含没有字体样式或大小的文本模式。我实际上想将我的特定字体大小和样式选项添加到那段文本中。

<body>
<div>No font style <span style="font-size: 14px;"><span style="font-family: verdana, helvetica, sans-serif;">And verdana 14  <span style="font-size: 18px;">And Verdana 18</span></span></span></div>

NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:@{...}     documentAttributes:nil error:&error];

最佳答案

请试试

NSMutableString *formattedContentString = [NSMutableString stringWithFormat:@"<style>*{font-family:%@;} !important</style> %@", @"Helvetica", htmlString];

更新:

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Your Font" size:20], NSFontAttributeName, nil]];

关于html - 字体解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675563/

相关文章:

javascript - 来自 API 的图像不显示

ios - "DateWithFormat"适用于终端但不适用于 iOS?

iphone - 将另一个 View 附加到同一 View "files owner"

ios - UIWebview(iOS)中的 Docx 支持?

javascript - 如何使用javascript实现验证?

javascript - 检查 jQuery 是否被内容过滤器阻止

javascript - 无法比较 JavaScript 中下拉框和文本字段中的值

android - 在 Hybrid 应用程序中本地下载 HTML 文件

ios - UISearchController:即使搜索栏为空也显示结果

ios - EXC_BAD_ACCESS中的code参数是什么意思?