I have an image called arrowWhite
. I want to colour this image to black.
func attachDropDownArrow() -> NSMutableAttributedString { let image:UIImage = UIImage(named: "arrowWhite.png")! let attachment = NSTextAttachment() attachment.image = image attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width - 2.25, attachment.image!.size.height - 2.25) let attachmentString = NSAttributedString(attachment: attachment) let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String) myString.appendAttributedString(attachmentString) return myString }
I want to get this image in blackColour
.tintColor
is not working...
The absolute simplest way to change colors of images (or icons in this case) is to use the SF Symbols where applicaple. This is a set of symbols Apple provides that can easily be used in your own app. You can download an app to help you find the correct symbol for you needs here.
If you are setting the image for a button, just go to attributes inspector and change the button type to system. Then set the image and change the tint color. The color of the image will change.
There are two ways to use your custom colors in Swift UI. Select your object in device preview. Choose “Color” under the attributes inspector. Your custom colors now show at the bottom of the list!
setBlendMode(. multiply) ctx. draw(image. cgImage!, in: imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }
Swift 4 and 5
extension UIImageView { func setImageColor(color: UIColor) { let templateImage = self.image?.withRenderingMode(.alwaysTemplate) self.image = templateImage self.tintColor = color } }
Call like this:
let imageView = UIImageView(image: UIImage(named: "your_image_name")) imageView.setImageColor(color: UIColor.purple)
Alternativ For Swift 3, 4 or 5
extension UIImage { func maskWithColor(color: UIColor) -> UIImage? { let maskImage = cgImage! let width = size.width let height = size.height let bounds = CGRect(x: 0, y: 0, width: width, height: height) let colorSpace = CGColorSpaceCreateDeviceRGB() let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)! context.clip(to: bounds, mask: maskImage) context.setFillColor(color.cgColor) context.fill(bounds) if let cgImage = context.makeImage() { let coloredImage = UIImage(cgImage: cgImage) return coloredImage } else { return nil } } }
For Swift 2.3
extension UIImage { func maskWithColor(color: UIColor) -> UIImage? { let maskImage = self.CGImage let width = self.size.width let height = self.size.height let bounds = CGRectMake(0, 0, width, height) let colorSpace = CGColorSpaceCreateDeviceRGB() let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) let bitmapContext = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) //needs rawValue of bitmapInfo CGContextClipToMask(bitmapContext, bounds, maskImage) CGContextSetFillColorWithColor(bitmapContext, color.CGColor) CGContextFillRect(bitmapContext, bounds) //is it nil? if let cImage = CGBitmapContextCreateImage(bitmapContext) { let coloredImage = UIImage(CGImage: cImage) return coloredImage } else { return nil } } }
Call like this:
let image = UIImage(named: "your_image_name") testImage.image = image?.maskWithColor(color: UIColor.blue)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With