简体   繁体   English

在iOS中的TextView中快速内联图像

[英]Inline images within textview in ios with swift

I'm wondering how to best implement a textView with inline images in my ios app. 我想知道如何在ios应用程序中以内嵌图像最好地实现textView。

I'm currently using a textView which is populated by a plist string. 我当前正在使用由plist字符串填充的textView。 I would like to add some images inline throughout the text and was wondering what would be a good solution. 我想在整个文本中内联添加一些图像,并且想知道什么是一个好的解决方案。 Do I need to make a webView or is there a simpler method? 我需要制作一个webView还是有一个更简单的方法?

If anyone can point me in the right direction, that would be fantastic. 如果有人能指出我正确的方向,那就太好了。 Thanks. 谢谢。

A fairly easy way to do this is using the textContainer's exclusion paths to remove the space where the text is able to occupy and placing an imageView there instead. 一种相当简单的方法是使用textContainer的排除路径来删除文本能够占据的空间,并在其中放置imageView。 It may look something like this: 它可能看起来像这样:

let image = UIImageView(image: UIImage(named: "Clock"))
let path = UIBezierPath(rect: CGRectMake(0, 0, image.frame.width, image.frame.height))
textView.textContainer.exclusionPaths = [path]
textView.addSubview(image)

And to make it read-only: 并将其设为只读:

textView.editable = false
textView.selectable = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM