字体解析问题

时间:2016-01-08 11:06:22

标签: 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];

1 个答案:

答案 0 :(得分:1)

请尝试

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