我想使用对象 - 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.
}
最终显示如下:
为什么会出现白色空白区域?
最佳答案
虽然我同意@Willeke的观点,即您应该使用 NSImageView,但您看到的是 NSTextField 的“边框”。
您可以通过添加来禁用它
text.isBezeled = false
到您的代码。
关于swift - NSTextField 中的图像显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49575516/