加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – NSTextAttachment图像未在NSTextView中显示(但在UITextVi

发布时间:2020-12-14 17:43:53 所属栏目:百科 来源:网络整理
导读:我在获取NSTextAttachment图像以在OS X应用程序的NSTextView中工作时遇到问题. NSTextAttachment的图像根本不显示.但是,它似乎仍然设置正确.因为复制NSTextView的内容并将其粘贴回例如TextEdit.app,粘贴的文本正确包含图像. 这是一个重现这个问题的最小游乐
我在获取NSTextAttachment图像以在OS X应用程序的NSTextView中工作时遇到问题.

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

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

import Cocoa

let img = NSImage(named: "Checked")

let textView = NSTextView(frame: NSMakeRect(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.两个游乐场都可以下载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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读