如何向 UITextView 添加图像/按钮?有自动换行功能吗?

发布于 2024-10-14 06:31:06 字数 183 浏览 10 评论 0原文

我想将图像/按钮添加到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-10-21 06:31:06

这绝对不是一件小事。对于初学者来说,你不能简单地将 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.

酒绊 2024-10-21 06:31:06

UITextViewattributedText 属性与带有图像的 NSAttributedStringNSTextAttachment 结合使用。请注意,UITextView 必须是Selectable(在 Storyboard 中)。

Swift 演示:

let attributedImage = NSAttributedString(with: #imageLiteral(resourceName: "myImage"))
let attributedText = NSMutableAttributedString(string: "Hello ")
attributedText.append(attributedImage)
textView.attributedText = attributedText

attributedImage 是从这些方便的初始化程序构建的:

extension NSAttributedString {
    convenience init(with image: UIImage) {
        self.init(attachment: NSTextAttachment(with: image))
    }
}
extension NSTextAttachment {
    convenience init(with image: UIImage) {
        self.init()
        self.image = image
        // adjust origin if needed
        self.bounds = CGRect(origin: .zero, size: image.size)
    }
}

此解决方案适用于:iOS 7.0、macOS 10.11、tvOS 9.0、*。

Use the attributedText property of UITextView with an NSAttributedString with NSTextAttachment with image. Note that the UITextView must be Selectable (in Storyboard).

Swift demonstration:

let attributedImage = NSAttributedString(with: #imageLiteral(resourceName: "myImage"))
let attributedText = NSMutableAttributedString(string: "Hello ")
attributedText.append(attributedImage)
textView.attributedText = attributedText

Where the attributedImage is built from those convenience initializers:

extension NSAttributedString {
    convenience init(with image: UIImage) {
        self.init(attachment: NSTextAttachment(with: image))
    }
}
extension NSTextAttachment {
    convenience init(with image: UIImage) {
        self.init()
        self.image = image
        // adjust origin if needed
        self.bounds = CGRect(origin: .zero, size: image.size)
    }
}

This solution works for: iOS 7.0, macOS 10.11, tvOS 9.0, *.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文