在 NSButton 中定位 NSImage
Positioning a NSImage within a NSButton
我有一个 NSButton
,以编程方式创建。在所述按钮中,我有一个用作图标的 NSImage:
let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
我想要做的是将图像设为 10pt。从按钮的左侧开始,同时垂直居中。到目前为止,图像水平和垂直居中显示,但由于我似乎无法定义框架或类似的东西,所以我无法真正移动它。
有没有办法在父级(按钮)内移动图像?
谢谢。
也许你可以在 NSButton
上使用 imagePosition
属性? (documented here).
它使用类型 NSCellImagePosition
(documented here) 的枚举,并允许您将图像设置为文本左侧、文本右侧、文本上方、文字等。
您仍然没有机会将事物像素完美对齐,但如果您可以接受,那么 imagePosition
似乎是可行的方法。
使用方法如下:
let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"
这给了我这个惊人的 UI:
希望对你有帮助。
我有一个 NSButton
,以编程方式创建。在所述按钮中,我有一个用作图标的 NSImage:
let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
我想要做的是将图像设为 10pt。从按钮的左侧开始,同时垂直居中。到目前为止,图像水平和垂直居中显示,但由于我似乎无法定义框架或类似的东西,所以我无法真正移动它。
有没有办法在父级(按钮)内移动图像?
谢谢。
也许你可以在 NSButton
上使用 imagePosition
属性? (documented here).
它使用类型 NSCellImagePosition
(documented here) 的枚举,并允许您将图像设置为文本左侧、文本右侧、文本上方、文字等。
您仍然没有机会将事物像素完美对齐,但如果您可以接受,那么 imagePosition
似乎是可行的方法。
使用方法如下:
let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"
这给了我这个惊人的 UI: