从html创建nsattributedstring时,ios7字体大小会发生变化

时间:2014-01-08 10:29:20

标签: html fonts ios7 nsattributedstring

我有一个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,因为它也将被其他平台使用。

有没有人知道为什么会这样?

5 个答案:

答案 0 :(得分:15)

我也遇到过这个问题,我通过迭代到属性并重新设置旧字体来修复它,如下所示

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

答案 1 :(得分:6)

使用<span>和css对我有用,遵循Parsing HTML into NSAttributedText - how to set font?

// HTML -> NSAttributedString
+ (NSAttributedString *)attributedStringFromHTML:(NSString *)html {
    NSError *error;
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:&error];
    if(!attrString) {
        NSLog(@"creating attributed string from HTML failed: %@", error.debugDescription);
    }
    return attrString;
}

// force font thrugh <span> & css
+ (NSAttributedString *)attributedStringFromHTML:(NSString *)html withFont:(UIFont *)font    {
    return [Util attributedStringFromHTML:[NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %f\";>%@</span>", font.fontName, font.pointSize, html]];
}

这会设置字体名称和大小,但不会影响样式。

答案 2 :(得分:4)

使用以下代码修复了这种神奇的Apple行为:

static CGFloat const kListsLeading = 10.0;
static CGFloat const kListsAdditionalShift = 4.0;

//
// Fix Apple magic 4/3 koefficient
// http://stackoverflow.com/questions/20992950/ios7-font-size-change-when-create-nsattributedstring-from-html
//
static NSAttributedString *DecreaseFontSizeBecauseOfAppleMagic(NSAttributedString *astr) {
    NSMutableAttributedString *mastr = [astr mutableCopy];

    [mastr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, astr.length) options:0 usingBlock:^(UIFont *_Nullable value, NSRange range, BOOL *_Nonnull stop) {
        if (value) {
            UIFont *font = [value fontWithSize:value.pointSize * 0.75];
            [mastr addAttribute:NSFontAttributeName value:font range:range];
        }
    }];

    [mastr enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, astr.length) options:0 usingBlock:^(NSParagraphStyle *_Nullable value, NSRange range, BOOL *_Nonnull stop) {
        if (value) {
            NSMutableParagraphStyle *style = [value mutableCopy];
            style.minimumLineHeight *= 0.75;
            if (style.firstLineHeadIndent == style.headIndent) {
                style.firstLineHeadIndent *= 0.75;
                style.headIndent *= 0.75;
            }
            else if (style.firstLineHeadIndent < kListsLeading) {
                CGFloat shift = (kListsLeading - style.firstLineHeadIndent);
                style.headIndent += shift + kListsAdditionalShift;
                style.firstLineHeadIndent = kListsLeading;
                NSMutableArray *tabs = [NSMutableArray array];
                NSInteger index = 0;
                for (NSTextTab *tab in style.tabStops) {
                    [tabs addObject:[[NSTextTab alloc] initWithTextAlignment:tab.alignment location:tab.location + shift + kListsAdditionalShift * (index ? 1 : 0) options:tab.options]];
                    index++;
                }
                style.tabStops = tabs;
            }
            style.tailIndent *= 0.75;
            [mastr addAttribute:NSParagraphStyleAttributeName value:style range:range];
        }
    }];

    return [mastr copy];
}

答案 3 :(得分:2)

Swift 3.0版本

0.75比率

yourAttrStr.beginEditing()
yourAttrStr.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, yourAttrStr.length), options: .init(rawValue: 0)) { 
        (value, range, stop) in
        if let font = value as? UIFont {
            let resizedFont = font.withSize(font.pointSize * 0.75)
            yourAttrStr.addAttribute(NSFontAttributeName, value: resizedFont, range: range)
        }
}
yourAttrStr.endEditing()//yourAttrStrwill be the same size as html string

答案 4 :(得分:1)

这不完全是一个答案,更多的是替代方案和对其他各种答案的评论,所以道歉。

@KishoreThindaak和@Danomatika给出的答案很好,如果你知道字体大小应该是什么 - 但我的应用程序有一个Mac OS双胞胎可以生成任何大小的文本,因此必须是通用的。

@ k06a给出的答案适用于简单文本,但我发现嵌入样式失败了 - 特别是在一行本身嵌入<li>标签中的多个样式。

经过几个小时的尝试解决这个问题之后,我担心我完全放弃了HTML作为磁盘格式并采用了RTF,它工作正常,并提供了一个可在所有平台上读取的RTF文件。获取RTF的简单代码......

extension NSAttributedString {
    func rtf(encoding: String.Encoding) -> Data? {
        let options: [NSAttributedString.DocumentAttributeKey : Any] = [
            NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtf,
            NSAttributedString.DocumentAttributeKey.characterEncoding: encoding.rawValue
        ]
        return try? self.data(from: NSMakeRange(0, self.length), documentAttributes: options)
    }
    class func from(rtfData data: Data, encoding: String.Encoding) -> NSAttributedString? {
        let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
            NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf,
            NSAttributedString.DocumentReadingOptionKey.characterEncoding: encoding.rawValue
        ]
        return try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil)
    }
}