简体   繁体   中英

NSTextAttachment image not shown in NSTextView (but in UITextView)?

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 eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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