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

 kingseao 发布于 2023-02-05 11:27

我有一个UITextView,我正在管理一个NSAttributedString,最初通过键盘正常输入.我将属性字符串保存为HTML,看起来很好.当我再次加载它,并将其转换回HTML中的属性字符串时,字体大小似乎发生了变化.

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











There is some text as usual lots of text

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

    // 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,因为它也将被其他平台使用.

有没有人有任何想法为什么会这样?

2 个回答
  • 使用<span>和css对我有用,将HTML解析为NSAttributedText - 如何设置字体?:

    // 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 font-family: %@; font-size: %f\";>%@</span>", font.fontName, font.pointSize, html]];
    }
    

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

    2023-02-05 11:29 回答
  • 我也遇到了这个问题,我通过迭代到属性并重新设置旧字体大小来修复它,如下所示

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

    2023-02-05 11:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有