Create a template image

I would add a template image at a button to change its color runtime.

I used Preview to draw and save it as a pdf.

But it is rendered only as a uniform square color (which color I can change runtimne).

How can I obtain the template pdf image?

Why the pdf saved by Preview is not working?

Answered by Luca_65 in 322820022

Yes, I want a template, a black and white image which color can be changed using setTitleColor(_ color: UIColor?, for state: UIControl.State).

I tested QuinceyMorris case 1, loading the pdf provided by Apple in https://developer.apple.com/go/?id=app-dev-swift-student-swift-4 and setting in Interface Builder "Render As" Template Image". It works.

If I change the pdf I obtain a square completely filled with the color desired. But a uniform square, not the image.

Thanks QuinceyMorris, the point is opacity. My pdf were without alpha channel. I modified it, using Gimp, so the white parts became transparent and it works.

Save image as png and tell if it works.

No, it doesn't.

I'm following the book "App Development with Swift" by Apple, there is the link https://developer.apple.com/go/?id=app-dev-swift-student-swift-4 which envelop a pdf which works, even with small change to it, made with preview, it works.

But with a new pdf the "template image" doesn't works.

What do you mean by "template"?


In macOS, a template NSImage is one whose colors are ignored, and the only significant information is the opacity (transparency) of the pixels. When you specify a template image for a button, the button colorizes the image with a standard color suitable for the button state (normal, highlighted, etc). You can't control which color is used.


If you want that behavior, then you must specify that the image is a template, and there are two ways to do this:


1. Load the NSImage and set its isTemplate property to true.


2. Load the NSImage using NSImage(named:), and specify an image file name that ends in the word "Template" (e.g. "MyImage Template.pdf", which you load as NSImage(named:"MyImage Template")).


Note that if you're setting the image name for a button in IB, you can use technique #2 to get it to be a template.

Accepted Answer

Yes, I want a template, a black and white image which color can be changed using setTitleColor(_ color: UIColor?, for state: UIControl.State).

I tested QuinceyMorris case 1, loading the pdf provided by Apple in https://developer.apple.com/go/?id=app-dev-swift-student-swift-4 and setting in Interface Builder "Render As" Template Image". It works.

If I change the pdf I obtain a square completely filled with the color desired. But a uniform square, not the image.

Thanks QuinceyMorris, the point is opacity. My pdf were without alpha channel. I modified it, using Gimp, so the white parts became transparent and it works.

Create a template image
 
 
Q