【问题标题】:Swift - Font size increases when converting html string to attributed string and back againSwift - 将 html 字符串转换为属性字符串并再次返回时,字体大小会增加
【发布时间】:2017-11-19 06:52:26
【问题描述】:

我有一个 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>

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

【问题讨论】:

标签: ios swift uitextview nsattributedstring


【解决方案1】:

我遇到了同样的问题,简单的解决方案是您只需转换为 RTF 格式而不是 HTML。效果很好。

【讨论】:

    【解决方案2】:

    试试这个我从一些帮助中找到的方法,然后转换为 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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 2015-01-19
      • 2013-02-18
      • 1970-01-01
      • 2011-10-30
      • 2018-02-05
      • 2012-11-01
      • 1970-01-01
      相关资源
      最近更新 更多