Add gaussian blur to layer backgroundColor?

Viewed 1883

I am trying to add a gaussian blur to a layer that uses backgroundColor.

The code below adds a box around the title text and works just fine, I just want the white to be blurred.

title.layer.borderColor = UIColor.white.cgColor
title.layer.backgroundColor = UIColor.white.cgColor
title.layer.borderWidth = 4
title.layer.cornerRadius = 5

Is this possible without using an imageView?

Thanks!

1 Answers

If you're coding for Mac OS then Vlad's solution below will work for you simply and elegantly. Mac OS CALayers let you add an array of Core Image filters which get applied "live" to the contents. iOS does not. Since you mention UIColor in your question I'm guessing that you're question is for iOS.

On iOS It would likely take a fair amount of manual coding using Core Image filters to do this with layers. (You'd need to write code to capture the contents of the view that you want to blur, convert it to a CIImage, hand that to Core Image to blur, and then map the resulting CIImage back to a CGImage and install it in your layer.)

However, if you add a UIVisualEffectView to your view that will let you blur the contents of the view that is underneath. A UIVisualEffectView is VERY easy to use. There are a number of tutorials online that show you how to use them. That's what I would suggest.

Related