我创建了一个UILabel并将其字体大小与搜索栏同步,并将html格式的文本加载到UILabel上。不幸的是,如果我使用seekbar更改UILabel的字体大小,则会删除该格式。

我使用了来自garie的这种方法,该方法是从另一个线程得到的

private func getHtmlLabel(text: String) -> UILabel {
    let label = UILabel()
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    label.attributedString = stringFromHtml(string: text)
    return label
}

private func stringFromHtml(string: String) -> NSAttributedString? {
    do {
        let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
        if let d = data {
            let str = try NSAttributedString(data: d,
                                             options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                             documentAttributes: nil)
            return str
        }
    } catch {
    }
    return nil
}

链接:Swift: Display HTML data in a label or textView

有什么方法可以用来保留html格式的文本?

编辑#1:
这就是我调整UILabel大小的方法。
@IBOutlet weak var textLabel: UILabel!
@IBAction func resizeText(_ sender: UISlider) {
    textLabel.font = UIFont.systemFont(ofSize: CGFloat(sender.value) * 20.0)
}

我传递给UILabel的HTML数据:
<html><body><b>hello world</b></body></html>

这是UILabel上格式化的HTML数据:

html - 使用HTML数据调整UILabel的大小-LMLPHP

调整UILabel的大小后,它将丢失其格式:

html - 使用HTML数据调整UILabel的大小-LMLPHP

编辑#2:

尝试米兰的答案,但遇到错误。 NSAttributedStringKey中的未解析标识符

这是我的代码:
import UIKit

class GalleryViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        textLabel.attributedText = stringFromHtml()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBOutlet weak var textLabel: UILabel!
    @IBAction func resizeText(_ sender: UISlider) {
        if let attributedText = textLabel.attributedText {
            let newAttributedText = NSMutableAttributedString(attributedString: attributedText)
            newAttributedText.setFontSize(newSize: CGFloat(sender.value) * 20.0)
            textLabel.attributedText = newAttributedText
        }
    }


    private func stringFromHtml() -> NSAttributedString? {
        do {
            let string = "<html><body><b>hello world</b></body></html>"
            let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
            if let d = data {
                let str = try NSAttributedString(data: d,
                                                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                 documentAttributes: nil)
                return str
            }
        } catch {
        }
        return nil
    }
}

extension NSMutableAttributedString {
    func setFontSize(newSize: CGFloat) {
        beginEditing()
        self.enumerateAttribute(NSAttributedStringKey.font, in: NSRange(location: 0, length: self.length)) { (value, range, stop) in
            if let f = value as? UIFont {
                let newFont = f.withSize(newSize)
                removeAttribute(NSAttributedStringKey.font, range: range)
                addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
            }
        }
        endEditing()
    }
}

最佳答案

设置字体会直接弄乱您的属性文本。您将必须在attributedText上设置字体大小:

@IBAction func resizeText(_ sender: UISlider) {
    if let attributedText = textLabel.attributedText {
        let newAttributedText = NSMutableAttributedString(attributedString: attributedText)
        newAttributedText.setFontSize(newSize: CGFloat(sender.value) * 20.0)
        textLabel.attributedText = newAttributedText
    }
}

为此,您需要对NSMutableAttributedString进行以下扩展:
extension NSMutableAttributedString {
    func setFontSize(newSize: CGFloat) {
        beginEditing()
        self.enumerateAttribute(NSAttributedStringKey.font, in: NSRange(location: 0, length: self.length)) { (value, range, stop) in
            if let f = value as? UIFont {
                let newFont = f.withSize(newSize)
                removeAttribute(NSAttributedStringKey.font, range: range)
                addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
            }
        }
        endEditing()
    }
}

10-04 23:07
查看更多