swift - NSTextField 中的图像显示不正确

标签 swift macos cocoa nstextfield textkit

我想使用对象 - NSTextField 来显示图像。但结果不正确。

图像尺寸为32x32。

我的代码是这样的:

class ViewController: NSViewController {

var text: NSTextField!

override func viewDidLoad() {
    super.viewDidLoad()

    text = NSTextField(frame: NSRect(origin: CGPoint(x: 200, y: 200), size: CGSize(width: 32, height: 32)))
    text.backgroundColor = .clear
    text.isBordered = true
    text.isEditable = false

    let attachment = NSTextAttachment()
    attachment.image = NSImage(named: NSImage.Name(rawValue: "Group_8"))
    attachment.bounds = NSRect(origin: .zero, size: (attachment.image?.size)!)

    let string = NSAttributedString(attachment: attachment)

    text.attributedStringValue = string

    view.addSubview(text)


    // Do any additional setup after loading the view.
}

最终显示如下:

enter image description here

为什么会出现白色空白区域?

最佳答案

虽然我同意@Willeke的观点,即您应该使用 NSImageView,但您看到的是 NSTextField 的“边框”。

您可以通过添加来禁用它

text.isBezeled = false

到您的代码。

关于swift - NSTextField 中的图像显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49575516/

相关文章:

ios - WKInterfaceTable 不创建行

ios - 游戏中心 : Player Groups

python - 用 Boost Python 包装 C++ 函数——numpy 数组类型

objective-c - NSWindow 调整大小

ios - Xcode - 如何修复 'NSUnknownKeyException',原因 : … this class is not key value coding-compliant for the key X"error?

objective-c - 延迟文档关闭

ios - UIView 前面的截图层

ios - swift 中的简单完成处理程序或回调

ruby - 错误 : inreplace failed while installling with homebrew?

ios - NSURLConnection 无响应