本文介绍了NSTextView中未显示NSTextAttachment图像(但在UITextView中显示)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使NSTextAttachment图像在OS X应用程序的NSTextView中工作时遇到问题.

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

这里是一个重现此问题的最小游乐场:

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

预期输出:

对于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

我正在使用XCode7.可以在此处下载两个游乐场./p>

任何想法都非常欢迎,谢谢!

解决方案
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)

I am having issues getting a NSTextAttachment image to work in a NSTextView for an OS X application.

The image of the NSTextAttachment is just not displayed at all. However, it still seems to be set correctly. Because when copying the contents of the NSTextView and pasting it back into e.g. TextEdit.app, the pasted text contains the image correctly.

Here is a minimal playground reproducing the issue:

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

Expected output:

For iOS, so using UIKit instead of Cocoa, it works perfectly fine:

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

I am using XCode 7. Both playgrounds can be downloaded here.

Any idea is highly welcome, Thanks in advance!

解决方案
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)

这篇关于NSTextView中未显示NSTextAttachment图像(但在UITextView中显示)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:56