How to draw a NSImage like images in NSButtons (with a deepness)?

前端 未结 4 1699
遥遥无期
遥遥无期 2020-12-22 18:37

Is there any way to draw an NSImage like images in NSButtons or other cocoa interface elements?

Here are examples:

4条回答
  •  爱一瞬间的悲伤
    2020-12-22 19:00

    Here's a much simpler solution: just create a cell and let it draw. No mucking around with private APIs or Core Graphics.

    Code could look similar to the following:

    NSButtonCell *buttonCell = [[NSButtonCell alloc] initImageCell:image];
    buttonCell.bordered = YES;
    buttonCell.bezelStyle =  NSTexturedRoundedBezelStyle;
    // additional configuration
    [buttonCell drawInteriorWithFrame: someRect inView:self];
    

    You can use different cells and configurations depending on the look you want to have (eg. NSImageCell with NSBackgroundStyleDark if you want the inverted look in a selected table view row)

    And as a bonus, it will automatically look correct on all versions of OS X.

提交回复
热议问题