Swift NSMutableAttributedString导致在事务中调用的+ [CATransaction synchronize]

时间:2017-05-30 13:34:43

标签: ios swift swift3

let TipNovice: Novica = NovicaBackend.novica! as! Novica
        let cell = tableView.dequeueReusableCell(withIdentifier: "Novica", for: indexPath) as! TableViewCell_Novica
        cell.message = NovicaBackend

        let htmlString = "<style>body {font-family: Raleway-Light; font-size: 15;}</style>\(TipNovice.body)"

        let encodedData = htmlString.data(using: String.Encoding.utf16)!
        let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]

        let attributedString = try! NSMutableAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

        let paragraphstyle = NSMutableParagraphStyle()
        paragraphstyle.lineSpacing = 3
        attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphstyle, range: NSMakeRange(0, attributedString.length))
        CATransaction.commit()
        cell.Novica_opis.attributedText = attributedString

我需要帮助处理在此代码被破坏时我得到的事务错误中调用的+ [CATransaction synchronize]。更确切地说,导致问题的唯一因素是

let attributedString = try! NSMutableAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

所有这些代码都在这个TableView函数中运行。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

1 个答案:

答案 0 :(得分:0)

我遇到类似的问题,在AdvancedDrawItem甚至NSAttributedString内调用viewDidLoad()导致viewWillAppear()发生。我通过将对+[CATransaction synchronize]的适当调用放入主队列来修复它,以便在不同的时间点执行。

像:

NSAttributedString

注意:DispatchQueue.main.async { self.textField.attributedText = attributedStringFromHtmlString(infoMessage) } 是一个调用attributedStringFromHtmlString

的包装函数

有关类似问题,另请参阅github