gpt4 book ai didi

ios - NSTextAttachment 图像未在 NSTextView 中显示(但在 UITextView 中)?

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:32 25 4
gpt4 key购买 nike

我在获取 NSTextAttachment 图像以在 OS X 应用程序的 NSTextView 中工作时遇到问题。

NSTextAttachment 的图像根本不显示。但是,它似乎仍然设置正确。因为在复制 NSTextView 的内容并将其粘贴回例如TextEdit.app,粘贴的文本正确包含图像。

这是重现该问题的最小 Playground :

import Cocoa

let img = NSImage(named: "Checked")

let textView = NSTextView(frame: NSMakeRect(0, 0, 254, 64))

let attrstr = NSMutableAttributedString(string: "Test")

let attch = NSTextAttachment()
attch.image = img

attrstr.appendAttributedString(NSAttributedString(attachment: attch))

textView.textStorage!.setAttributedString(attrstr)

textView

enter image description here

预期输出:

enter image description here

对于 iOS,所以使用 UIKit 而不是 Cocoa,它工作得非常好:

import UIKit

let img = UIImage(named: "Checked")

let textView = UITextView(frame: CGRectMake(0.0, 0.0, 200.0, 44.0))

let attrstr = NSMutableAttributedString(string: "Test")

let attch = NSTextAttachment()
attch.image = img

attrstr.appendAttributedString(NSAttributedString(attachment: attch))

textView.attributedText = attrstr

textView

enter image description here

我正在使用 XCode 7。两个 playground 都可以下载 here .

非常欢迎任何想法,提前致谢!

最佳答案

var thumbnailImage: NSImage? = // some image
var attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.initImageCell(thumbnailImage!)
var attachment: NSTextAttachment = NSTextAttachment()
attachment.attachmentCell = attachmentCell
var attrString: NSAttributedString = NSAttributedString.attributedStringWithAttachment(attachment)
self.textView.textStorage().appendAttributedString(attrString)

关于ios - NSTextAttachment 图像未在 NSTextView 中显示(但在 UITextView 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com