如何在 Swift 3 中不丢失换行符的情况下将 HTML 文本转换为属性字符串 [英] How to transfer HTML text into an attributed string without losing line breaks in Swift 3

查看:76
本文介绍了如何在 Swift 3 中不丢失换行符的情况下将 HTML 文本转换为属性字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要标记为重复.现有问题都不能解决不丢失换行符.

Please do not mark as duplicate. None of the existing questions solves not losing line breaks.

给定字符串:"常规文本变为<b>粗体&nbsp;</b>\n\n<b><i>斜体</i></b>\n\n换行符"

Given String: "Regular text becomes <b>bold with&nbsp;</b>\n\n<b><i>An italic</i></b>\n\n<b>Linebreak</b>"

我有两个选择:

let attrStr = try! NSAttributedString(
        data: story.body.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                   NSFontAttributeName: Handler.shared.setFont(FontNames.sourceSerifProRegular, 25.0)],
        documentAttributes: nil)

此选项会丢失字体、大小换行符.

This option loses the font, size and the line breaks.

以下此答案的扩展,保留了UILabel 的字体,但丢失了换行也是如此.

The following extension from this answer, keeps UILabel's font, but it loses the line breaks as well.

extension UILabel {
    func _slpGetSize() -> CGSize? {
        return (text as NSString?)?.size(attributes: [NSFontAttributeName: font])
    }

    func setHTMLFromString(htmlText: String) {
        let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span>" as NSString, htmlText) as String

        let attrStr = try! NSAttributedString(
            data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
            documentAttributes: nil)

        self.attributedText = attrStr
    }
}

label.setHTMLFromString(htmlText: story.body)

我错过了什么?我需要做什么来保持换行符?非常感谢帮助.

What am I missing? What do I need to do to keep the line breaks? Help is very appreciated.

推荐答案

尝试在 UILabel 中设置 numberOfLines 属性.

Try set numberOfLines property in the UILabel.

为此,您可以计算断行数并设置为 numberOfLines.

For that, you can count the number of break lines and set into numberOfLines.

extension UILabel {
    func _slpGetSize() -> CGSize? {
        return (text as NSString?)?.size(attributes: [NSFontAttributeName: font])
    }

    func setHTMLFromString(htmlText: String) {
        let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span>" as NSString, htmlText) as String

        let attrStr = try! NSAttributedString(
            data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
            documentAttributes: nil)

        self.attributedText = attrStr

        self.numberOfLines = htmlText.components(separatedBy: "\n").count
    }
}

希望这个例子对你有帮助.

I hope this example help you.

这篇关于如何在 Swift 3 中不丢失换行符的情况下将 HTML 文本转换为属性字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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