swift - 如何在 swift 中使用 DTCoreText (DTWillFlushBlockCallBack)

标签 swift objective-c-blocks dtcoretext

        let callBackBlock:(DTHTMLElement) -> Void = { element in
        for oneChildElement in element.childNodes {
            guard let oneChildElement: DTHTMLElement = oneChildElement as? DTHTMLElement else {
                return
            }
            if oneChildElement.displayStyle == .inline && oneChildElement.textAttachment.displaySize.height > 2.0 * oneChildElement.fontDescriptor.pointSize {
                oneChildElement.displayStyle = .block;
                oneChildElement.paragraphStyle.minimumLineHeight = element.textAttachment.displaySize.height;
                oneChildElement.paragraphStyle.maximumLineHeight = element.textAttachment.displaySize.height;
            }
        }
    }
    let maxImageSize = CGSize(width: view.bounds.size.width - 20, height: view.bounds.size.height - 20)
    return [NSTextSizeMultiplierDocumentOption: NSNumber(floatLiteral: 1.0),
                                                DTMaxImageSize: NSValue(cgSize: maxImageSize),
                                                DTDefaultTextColor: UIColor.white,
                                                DTDefaultFontSize: NSNumber(floatLiteral: 15.0),
                                                DTWillFlushBlockCallBack: callBackBlock]

我像这样在 swift 中使用 block ,但是 block 因 exc_bad_access 而崩溃

enter image description here

最佳答案

你可以像这样使用 DHTMLAttributedStringBuilder 来生成 attributedString

    let options = [String: Any]() # other options
    let callBack: (DTHTMLElement?) -> Void = { element in
      #do something
      print(element)
    }
    if let builder = DTHTMLAttributedStringBuilder(html: data, options: options, documentAttributes: nil) {  
      builder.willFlushCallback = callBack
      if let attribute = builder.generatedAttributedString() {
        print(attribute)
      }
    }



关于swift - 如何在 swift 中使用 DTCoreText (DTWillFlushBlockCallBack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670092/

相关文章:

ios - 如何处理 UIImageview 的 UIScrollView 缩放中的偏移量?

ios - Objective-C:用作子函数的 block 的命名实践

ios - 如何在 DTCoreText 中正确呈现嵌套/多个上标?

ios - 使用 DTCoreText 显示 HTML 代码时出错

ios - AlamofireObjectMapper 无法推断通用参数 'T'

swift - 在命令行上使用 Swift 导入

ios - 如何在自定义单元格中切换开关时调用函数 - TableViewCell Swift

ios - 强 block 捕获 'self'

ios - EXC_BAD_INSTRUCTION 在 dispatch_get_current_queue()