当从html创建nsattributedstring时,ios7字体大小发生变化 [英] ios7 font size change when create nsattributedstring from html

查看:137
本文介绍了当从html创建nsattributedstring时,ios7字体大小发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITextView where I'm managing an NSAttributedString, initially entered as normal via the keyboard. I save the attributed string as HTML, which looks fine. When I load it again, and convert it back to an attributed string from the HTML, the font size appears to change.

例如,HTML加载看起来像这样:

For example, the HTML on loading looks like this:

<!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>

我将其转换并使用以下代码检查属性:

I convert it and check the attributes with the following code:

    // 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:

The output shows that the font size has increased from 21 to 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,因为它也将被其他平台使用。

Basically, each time I load the string, the font size increases. I need to store it as HTML rather than as NSData because it will also be used by other platforms.

有没有人有任何想法,为什么发生这种情况?

Does anyone have any ideas why this is happening?

推荐答案

我也遇到了这个问题,我通过迭代到旧的字体大小来重新设置它,如下所示: b

I also faced this issue, I fixed it by iterating to the attributes and reseting the old font size as follows

NSMutableAttributedString *res = [attributedText mutableCopy];
[res beginEditing];
[res enumerateAttribute:NSFontAttributeName
                inRange:NSMakeRange(0, res.length)
                options:0
             usingBlock:^(id value, NSRange range, 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];

这篇关于当从html创建nsattributedstring时,ios7字体大小发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆