ios - Swift - 将 html 字符串转换为属性字符串并再次返回时,字体大小会增加

标签 ios swift uitextview nsattributedstring

我有一个 textView,允许用户输入 NSAttributedString,转换成 html 字符串,然后存储到数据库中。我还有将 html 字符串转换回并显示在 textView 中进行编辑的代码。我的转换代码是

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
        guard let html = try? NSMutableAttributedString(
            data: data,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil) else { return nil }
        return html
    }
}

extension NSAttributedString {
    func htmlString() -> String? {
        let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
        do {
            let htmlData = try self.data(from: NSMakeRange(0, self.length), documentAttributes:documentAttributes)
            if let htmlString = String(data:htmlData, encoding:String.Encoding.utf8) { return htmlString }
        }
        catch {}
        return nil
    }
}

但是问题是,每次我保存和显示字符串时,所有字体大小都会增加。比如我有一个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: 17.0px '.SF UI Text'; color: #000000}
span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 17.00pt}
</style>
</head>
<body>
<p class="p1"><span class="s1">text</span></p>
</body>
</html>

在我将它转换为 NSAttributedString 并再次转换回来后,其他一切都一样,但 css 行更改为此。

<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 28.0px; font: 22.7px '.SF UI Text'; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 22.67pt; font-kerning: none}
</style>

我错过了什么吗?任何帮助表示赞赏!

最佳答案

试试这个我从一些帮助中找到的方法,然后转换为 Swift 3:

func newAttrSize(blockQuote: NSAttributedString) -> NSAttributedString
{
    let yourAttrStr = NSMutableAttributedString(attributedString: blockQuote)
    yourAttrStr.enumerateAttribute(.font, 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(.font, value: resizedFont, range: range)
        }
    }

    return yourAttrStr
}

关于ios - Swift - 将 html 字符串转换为属性字符串并再次返回时,字体大小会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590184/

相关文章:

ios - AFNetworking 的 setImageWithURLRequest 在滚动后将图像设置在错误的单元格中(iOS、Swift)

ios7 - 防止在 presentViewController 上隐藏键盘(显示弹出窗口)

string - 在不同 View 上显示输入文本

swift - 属性文本,使用 swift 将特定字体替换为另一种字体

ios - 未调用 UISearchDisplayDelegate 方法

ios - 如何配置 cellForRowAtIndexPath 以处理两种不同的自定义 UITableViewCell 类型

ios - 在 iOS 应用中,客户可以看到 Bundle Id 或 SKU

ios - 基于关系获取数据的核心数据谓词给出错误“无法解析格式字符串”

ios - 如何创建将数据插入 block 的函数

ios - 自定义输入 View 执行。除非打开默认键盘,否则自定义 inputView 不会出现