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 而崩溃
最佳答案
你可以像这样使用 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/