从html创建nsattributedstring时,ios7的字体大小更改

我有一个UITextView,我正在管理一个NSAttributedString,最初通过键盘正常输入.我将归因的字符串保存为 HTML,看起来不错.
当我再次加载它,并将其转换回归属的字符串从HTML,字体大小似乎改变.

例如,加载的HTML如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<Meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<Meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 21.0px Helvetica; color: #000000; -webkit-text-        stroke: #000000}
span.s1 {font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size:     21.00pt;     font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">There is some text as usual lots of text</span></p>
</body>
</html>

我转换它并使用以下代码检查属性

// convert to attributed string
    NSError *err;
    NSAttributedString *as = [[NSAttributedString alloc] initWithData:data3
                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                            documentAttributes:nil
                            error:&err] ;

    NSMutableAttributedString *res = [as mutablecopy];
    [res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0,res.length) options:0 usingBlock:^(id value,NSRange range,BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            NSLog(@"On Loading: Font size %f,in range from %d length %d",oldFont.pointSize,range.location,range.length);
        }
    }];

输出显示字体大小从21增加到28:

On Loading: Font size 28.000000,in range from 0 length 40
On Loading: Font size 21.000000,in range from 40 length 1

基本上每次加载字符串时,字体大小都会增加.我需要将其存储为HTML而不是NSData,因为它也将被其他平台使用.

有人有什么想法为什么会发生这种情况吗?

解决方法

我也遇到这个问题,我通过迭代属性修复它,并重新排列旧的字体大小如下
NSMutableAttributedString *res = [attributedText mutablecopy];
[res beginEditing];
[res enumerateAttribute:NSFontAttributeName
                inRange:NSMakeRange(0,res.length)
                options:0
             usingBlock:^(id value,BOOL *stop) {
                 if (value) {
                     UIFont *oldFont = (UIFont *)value;
                     UIFont *newFont = [oldFont fontWithSize:15];
                     [res addAttribute:NSFontAttributeName value:newFont range:range];
                 }
             }];
[res endEditing];
[self.textFileInputView setAttributedText:res];

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些