如何向 UITextView 添加图像/按钮?有自动换行功能吗?
我想将图像/按钮添加到我的 UITextView 中,文本应自动换行以适合图像/按钮。
第二个问题。我想制作一个名为“移动图像/按钮”的按钮,然后用户可以通过 UITextView 移动图像/按钮,并且文本应该调整
。
PS:就像 Mac 上的“Pages”或 Windows 上的“Word”
I want to add an image/button to my UITextView and the text should automatically make a line break that the image/button fits.
Second question. I want to make a button called "move image/button" and then the user can move the image/button through the UITextView and the text should adjust
.
PS: like in "Pages" for mac or "Word" for Windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这绝对不是一件小事。对于初学者来说,你不能简单地将 UIImage 嵌入到 UITextView 中,即使你可以,文本也不会神奇地在它周围流动。您需要做的是基于 UITextView 创建自己的对象,该对象大大扩展了其功能以提供此类编辑工具。
要了解您要进入的内容,您可能需要查看 来自 Omni Group 的示例文本编辑器源代码。
This is decidedly non-trivial. For starters, you can't simply embed a UIImage inside a UITextView and even if you could text wouldn't magically flow around it. What you'll need to do is create your own object based on a UITextView that substantially extends its capabilities to provide such editing tools.
For a heads-up as to what you're getting into you might want to take a look at the example Text Editor source code from the Omni Group.
将
UITextView
的attributedText
属性与带有图像的NSAttributedString
和NSTextAttachment
结合使用。请注意,UITextView
必须是Selectable
(在 Storyboard 中)。Swift 演示:
attributedImage
是从这些方便的初始化程序构建的:此解决方案适用于:iOS 7.0、macOS 10.11、tvOS 9.0、*。
Use the
attributedText
property ofUITextView
with anNSAttributedString
withNSTextAttachment
with image. Note that theUITextView
must beSelectable
(in Storyboard).Swift demonstration:
Where the
attributedImage
is built from those convenience initializers:This solution works for: iOS 7.0, macOS 10.11, tvOS 9.0, *.