To display remote images, use DTLazyImageView within the DTAttributedTextContentView delegate method viewForAttachment(_:frame:). When the image loads, you must update the attachment's originalSize and trigger a relayout on the content view.
// 1. Return the lazy image view in the delegate
func attributedTextContentView(
_ attributedTextContentView: DTAttributedTextContentView,
viewForAttachment attachment: DTTextAttachment,
frame: CGRect
) -> UIView? {
guard let imageAttachment = attachment as? DTImageTextAttachment else { return nil }
let imageView = DTLazyImageView(frame: frame)
imageView.delegate = self
imageView.url = imageAttachment.contentURL
return imageView
}
// 2. Handle image load and relayout
func lazyImageView(_ lazyImageView: DTLazyImageView, didChangeImageSize size: CGSize) {
guard let url = lazyImageView.url else { return }
let pred = NSPredicate(format: "contentURL == %@", url as CVarArg)
for attachment in attributedTextContentView.layoutFrame.textAttachments(with: pred) {
attachment.originalSize = size
}
attributedTextContentView.layayer = nil
attributedTextContentView.relayoutText()
}