如何在Swift 3中的DTCoreText中用html内联显示图像

时间:2017-07-25 09:17:22

标签: swift dtcoretext

我有DTAttributedTextContentView,我成功地从HTML渲染文本。现在我想嵌入一个内嵌文字的图片。我查看了文档,只有Objective-C示例。如何在Swift上做同样的事情:

显示远程图像的最佳方法是使用DTLazyImageView。首先,您需要为图像附件返回DTLazyImageView实例。

httpQequest.query(param)

然后在DTLazyImageView的in delegate方法中重置受影响的DTAttributedContextView的布局。

- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForAttachment:(DTTextAttachment *)attachment frame:(CGRect)frame
{
    if([attachment isKindOfClass:[DTImageTextAttachment class]])
     {
        DTLazyImageView *imageView = [[DTLazyImageView alloc] initWithFrame:frame];
        imageView.delegate = self;

        // url for deferred loading
        imageView.url = attachment.contentURL;
        return imageView;
    }
    return nil;
}

similar question is here

1 个答案:

答案 0 :(得分:0)

我最后将其重写为Swift 3语法:

  @IBOutlet weak var textLabel: DTAttributedTextContentView!


        func attributedTextContentView(viewForAttachment: DTAttributedTextContentView, attachment: DTTextAttachment, frame: CGRect) -> UIView {
            if attachment is DTImageTextAttachment {
                let imageView = DTLazyImageView(frame: frame)
                imageView.delegate = self as! DTLazyImageViewDelegate
                // url for deferred loading
                imageView.url = attachment.contentURL
                return imageView
            }
            return UIView(frame: CGRect.zero)
        }

        func lazyImageView(lazyImageView: DTLazyImageView, didChangeImageSize: CGSize) {
            guard let url = lazyImageView.url else {return}
            let pred = NSPredicate(format: "contentURL == %@", url as CVarArg)

            let array = textLabel.layoutFrame.textAttachments(with: pred)



            for (_, _) in (array?.enumerated())! {
                let element = DTTextAttachment()
                element.originalSize = didChangeImageSize
            }

            textLabel.layouter = nil
            textLabel.relayoutText()
        }
相关问题