Custom Button in Swift and MacOS - macos

In MacOS there is a simple App called Calculator, that I would like to recreate using Swift for learning purposes. A button like the plus button on the calculator has two images associated with it, one when the button is up and one when the button is down, as seen on the images below.
As you can see, when the + button is pressed down, its image changes to a darker orange color and the text gets a dark grey color. My question is: How to implement this button behavior?
Is it possible to do it with NSButton (and if so how)? or is it easier to implement it using CALayer? Or maybe there is some other way that I have not thought of?

You should be able to use a single image and configure it in code or in your asset catalogue to be a "template image". That means the shape is taken into consideration, much like a stamp, and the stamped-out area is filled with color dynamically. That means you don't have to provide a white and dark gray/black variant. One variant will suffice, usually black to see the lines well, and the rest can be configured through.
See the SO question "How to NOT highlight the NSButton's template image when clicked?" for details about the setup: How to NOT highlight the NSButton's template image when clicked?

Related

XCODE 11, SWIFT 5. Drawing Lines, Boxes etc

I have recently switched to Xcode 11 and Swift 5. Also switched my entire project from UISwift to Storyboard as I read it has many advantages.
Now I’m finding disadvantages, I can’t draw boxes/lines on my storyboard, I can do it using the code but that creates further problems with making changes to my project in storyboard and element constrains.
Does anyone know if it’s possible to draw background boxes/lines between text? In some instances I have 6 labels that would all go under one box, so I can’t use label background feature.
See the online example below, of grey boxes
Now I’m finding disadvantages, I can’t draw boxes/lines on my storyboard
The storyboard editor isn't a drawing program -- it's mainly for laying out views and connecting them to each other and to other objects e.g. view controllers. If you're trying to use it to draw lines and boxes, you're barking up the wrong tree.
The storyboard editor does let you configure the views you create, so for example you can set the image displayed by a UIImageView, or the text displayed in a label, or the background color of any view. The gray boxes in your example are easy to do by just setting the background color of a view to gray. Or better, use a table to display those views, and programmatically set the background color of the cells depending on their row.
Does anyone know if it’s possible to draw background boxes/lines between text?
There are some hacky options. For example, you could very easily create a view class that draws a horizontal or vertical line through it's center point, or a view class that draws a line around its perimeter for a box. (You can actually use key-value coding to set properties on the view's layer to do this even without creating a subclass, but it's not something you want to have to do every time you need a box.) Those are fine for occasional use, but if you have any complex drawing to do, it's probably time to write a view class that draws the necessary content in code.
In some instances I have 6 labels that would all go under one box, so I can’t use label background feature.
Those 6 labels should all be contained inside another view, so you'd just set the background color for the view that contains them. Again, from your example, it looks like those are rows in a table. Each row in a table is its own view (or "cell"), and it's easy to set the background color in the same code that configures the rest of the subviews in that cell.

Toggle NSToolbarItem between Black and Blue

I'd like to make an NSToolbarItem with a custom image which toggles between Black and Blue, like the icons at the top right of Xcode 5's main window.
Peter Hosey suggested in this answer that it was as simple as setting the image to be a template, but I haven't found that to be the case. I set it to a template image, and that causes it properly render the dark gray gradient, but it doesn't render in blue when toggled.
I uploaded a very simple example of what I've done, here. If someone could tell me how to change it to light up the NSToolbarItem in blue, as Xcode does, then I'd very much appreciate it.
You mostly have everything you need in place, with the exception of providing an NSButton object using the style NSTexturedRoundedBezelStyle, with which you’ve associated your image.
↳ modified example project here
⌘ OS X Human Interface Guidelines | Window-Frame Controls

Draggable element in KineticJS that is "under" another?

I'm wanting to have a template of a Magic-type card (but with an irregularly shaped art section), where a user can upload their own image and drag it around to place it, but the template will still be in the foreground. I know with the EaselJS library, by default the "hit area" for clicks (to trigger things like click and drag) is only the visible pixels, so you could click "through" the transparent portion of a PNG and drag an element that is under it. So I'm looking for something like that with KineticJS. How would I do that?
Figured it out. createHitRegion is what I was looking for.

wxPython Button Background Image

Can I create a button with text with a background image? In wxPython, there are certain functions which create buttons such as wx.lib.buttons.GenBitmapTextButton and other functions like that. I'm wondering if I can create a button with a fancy background image, as well as label text.
There is no built-in button that allows a background image underneath the text. You can probably look at one of the generic button widgets though and use them as a template. Basically you just need to draw the bitmap on and then draw text on top, probably by using wx.DC or wx.GCDC or similar. The generic buttons include such things as wx.lib.buttons, wx.lib.agw.aquabutton and wx.lib.platebtn among others.

Cocoa: Custom control not limited to window frame - how to start?

I want to build a custom control that would work like this:
You have a kind of NSButton with an image.
You click the button and than appears a big square with a grid of photos.
You click one of the photos and it is set up as new image for the button. (square dissapears)
Now, how to draw this big square with photos if I want it not to be limited to window frame?
I mean, if the button was close to window border the square is going to be partially outside window. I would also like to add some shadow to the square and an animation for opening/closing.
One important thing: I want to be able to draw not only a square but any other simple shape (circle)!
This isn't really a drawing question so much as a general custom views question. It's important to make that distinction.
I'll describe this in terms of rectangles to give you the general idea*. You should make sure you understand the view hierarchy and view geometry in Cocoa. Without this important requisite knowledge, you'll remain dead in the water.
It's easy to set an NSButton's image, so I'll leave that to you. Your button's action, however, would tell some controller to show the "image picker" for the given button. Your image picker would be some type of borderless window with an image list inside. The image picker could be an IKImageBrowserView (you'll have to enable Image Kit in Interface Builder for this control to appear), which gives you an iPhoto-like grid of images (with/without titles, different border types, etc.).
An explanation of the operation of this controller and how it creates the window, manages the selection, and sets the button's image is very broad so if you get hung up on any of those steps, you'll need to create a separate question for each problem, otherwise this answer would have to be an instruction manual for writing your app for you.
* Your problem is a little more difficult because of your desire to have differently-shaped "popup windows" ... you'd have to make sure your available photos fit neatly within the shape so none of them are cut off. Armed with the basic knowledge of view geometry, I'll leave this to you as an exercise. A hint: you can use a borderless, transparent window to host a view that draws itself in any shape you please.

Resources