1

我有一个可编辑的 UITextView,我想在其中粘贴来自 youtube 的视频。我需要在编辑过程中播放视频。现在我只制作了我想播放的视频的快照。如何覆盖 NSTextAttachment 以粘贴按钮而不是图像来处理点击我的视频。

4

1 回答 1

0

我找到了解决方案,如果有人感兴趣,首先获取图像附件框架

class WATextAttachment: NSTextAttachment {

var completionHandler:((CGRect)->Void)?

override func attachmentBoundsForTextContainer(textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
    let superRect = super.attachmentBoundsForTextContainer(textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex)
    completionHandler?(lineFrag)
    return superRect
}

}

并附上视频快照后

let buttton = UIButton(frame: CGRectZero)
    buttton.titleLabel?.textColor = UIColor.clearColor()
    buttton.titleLabel?.text = link
    buttton.addTarget(self, action: #selector(CreateArticleViewController.openVideo(_:)), forControlEvents: .TouchUpInside)
    _textView.addSubview(buttton)
    imageAttach.completionHandler = {(rect : CGRect) in
        if !CGRectEqualToRect(buttton.frame, rect) {
            var frame = rect
            frame.size.height = self.youtubeView.frame.size.height
            buttton.frame = frame
        }
    }
于 2016-07-20T11:00:05.997 回答