The main problem I encountered is my understanding. Instead of making a color image and trying to make a transparent hole in it, we can simply fold it the other way around. So we have a colored background in the back, and then an image in front that has a mask to display only the text part. And actually, it's pretty simple if you are using iOS 8+ using the maskView UIView property.
So it might look something like this:
let coloredBackground = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100)) coloredBackground.backgroundColor = UIColor.greenColor() let imageView = UIImageView(frame: coloredBackground.bounds) imageView.image = UIImage(named: "myImage") coloredBackground.addSubview(imageView) let label = UILabel(frame: coloredBackground.bounds) label.text = "stackoverflow" coloredBackground.addSubview(label) imageView.maskView = label
source share