Gradient Transparent view swift - ios

im trying to make a gradient transparent view alert as shown in the picture below, any help ?
#IBInspectable var InsideColor: UIColor = UIColor.clear
#IBInspectable var OutsideColor: UIColor = UIColor.clear
override func draw(_ rect: CGRect) {
let colors = [InsideColor.cgColor, OutsideColor.cgColor] as CFArray
let endRadius = min(frame.width, frame.height) / 2
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}

It is better to have it statically in your project assets instead of creating it dynamically every time. So:
Create an image with the clear center and black fill color like this:
Note that the center part is NOT white. It's transparent. So you should see behind it like this example:
Then add an imageView and fill it with this image.
Set the backgroundColor of the image view to .clear
done.
Note: Don't forget to set the backgroundColor of the original view of your custom alert to .clear and modalPresentationStyle of the custom alert controller to .overCurrentContext

Related

How give gradient on text in TextView iOS?

I would like to give gradient effect on text in UITextView, when I try using gradient layer its applied on complete textview background. I just want it tp be applied on text and background coloraturas should separate.
I want output like this :
And its coming like this:
Can someone suggest how to achieve output like first image on UITextView
I am specifically looking solution for UITextView, instead UILabel or UIView
You can create a patterned color for your text. So, you can apply this color to any text component(like a label,textView, button, etc).
Please check the below example. where you can customize your color pattern in the getGradientLayer() method.
func gradientColor(bounds: CGRect, gradientLayer :CAGradientLayer) -> UIColor? {
//We are creating UIImage to get gradient color.
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return UIColor(patternImage: image!)
}
func getGradientLayer(bounds : CGRect) -> CAGradientLayer{
let gradient = CAGradientLayer()
gradient.frame = bounds
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
return gradient
}
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
textView.font = UIFont.boldSystemFont(ofSize:50)
textView.textAlignment = .center
textView.text = "Hello World!"
let gradient = getGradientLayer(bounds: textView.bounds)
textView.textColor = gradientColor(bounds: textView.bounds, gradientLayer: gradient)
Output:-
Easiest way to achieve this is to create a UIView with the desired gradient, over your UILabel or UITextView and then mask the gradient UIView over the textView.
There is a good tutorial here.

Apply Beveled Radial Gradient to UIView

I'm currently using the following code here
#IBDesignable class RadialGradientView: UIView {
#IBInspectable var outsideColor: UIColor = UIColor.red
#IBInspectable var insideColor: UIColor = UIColor.green
override func draw(_ rect: CGRect) {
let colors = [insideColor.cgColor, outsideColor.cgColor] as CFArray
let endRadius = sqrt(pow(frame.width/2, 2) + pow(frame.height/2, 2))
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
This code will draw a radial gradient that looks like this:
What I am trying to do is make it look like below (which is how it looks in the current desktop version of the app).
You will notice that in the desktop version the gradient has blue at the top and bottom and the white area is stretched to include more of the text area so it's easier to read.
How can I recreate this effect? I have tried for hours playing with different numbers and settings in the current code to no avail so far (such as changing the endRadius).
Any help or ideas are appreciated.

How to attain opacity in a shape drawn in a layer of UIView that is a subView of a UIImageView?

I have an image presented in a UIImageView which takes up the entire view of the UIViewController. I would like to draw a circle(fill: yellow) in the layer of a subview(UIView) of this UIImageView. My problem is the following, how do I make the opacity of this yellow circle such that it can see through to the background image (e.g opacity 0.2). I have tried the following without success. All I get is a solid yellow circle on top of the background image. I need this yellow circle to be in the layer of the subView as described, rather than in a layer of the UIImageView.
let dotSize: CGFloat = 30
var fourthDotView: UIView?
#IBOutlet weak var imgPreViewOutlet: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
fourthDotView = UIView.init()
fourthDotView?.frame = CGRect.init(x: fourthOrigin.x, y: fourthOrigin.y, width: dotSize, height: dotSize)
drawFourthDot()
imgPreViewOutlet.addSubview(fourthDotView!)
}
// DRAW YELLOW CIRCLE
func drawFourthDot() -> Void {
let layer = CAShapeLayer.init()
layer.path = UIBezierPath.init(roundedRect: fourthDotView!.bounds, cornerRadius: 50).cgPath
layer.fillColor = UIColor.init(red: 255, green: 255, blue: 0, alpha: 0.2).cgColor
fourthDotView?.layer.addSublayer(layer)
}
For setting the opacity, you should use the opacity property.
layer.opacity = 0.20

Radial gradient uses unexpected gray color

TL;DR
Does anyone know how I can smooth out the gradient transition from red to white in a radial gradient? Because I'm getting an ugly gray color in my gradient.
Details:
I created a custom UIView with the following in draw rect
override func draw(_ rect: CGRect) {
let colors = [UIColor.red.cgColor, UIColor.clear.cgColor] as CFArray
let divisor: CGFloat = 3
let endRadius = sqrt(pow(frame.width/divisor, 2) + pow(frame.height/divisor, 2))
let center = CGPoint(x: bounds.midX, y: bounds.midY)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.saveGState()
context?.drawRadialGradient(gradient!,
startCenter: center,
startRadius: 0.0,
endCenter: center,
endRadius: endRadius,
options: .drawsBeforeStartLocation)
context?.restoreGState()
}
I set the custom view's color to red and added the view to the viewcontroller's view, which has a white background.
I wanted the gradient to go from red to white but it seems to go from red to gray. I tried changing the second color in the gradient array to white, and though it did look slightly better, the grayness still remains.
When using clear color on transparent gradients keep in mind that the clear color is generated by applying the black color an alpha and thats why you get the greyish color on your case.
To solve this in your code change UIColor.clear.cgColor, from colors array, with UIColor.white.withAlphaComponent(0.0).cgColor. This way you can get the gradient you expect.

Add gradient color on border of View

I am using swift 2.2 .I have a view and i want to use gradient color on inner border sides of view.Please help me.I am using the following code.Here i attach the screenshot which i need view.
self.layer.borderWidth = 1
self.layer.borderColor = UIColor(white:0.1, alpha: 0.5).CGColor
try this code
#IBOutlet weak var gradientView: UIView!
gradientView.backgroundColor = UIColor.clearColor()
let gradient:CAGradientLayer = CAGradientLayer()
gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
gradient.endPoint = CGPoint(x: 0.0, y: 0.0)
gradient.frame.size = self.gradientView.frame.size
gradient.colors = [UIColor.darkGrayColor().CGColor,UIColor.darkGrayColor().colorWithAlphaComponent(0).CGColor] //Or any colors
gradient.locations = [ 0.0, 1.0]
self.gradientView.layer.insertSublayer(gradient, atIndex: 0)
iPatel's answer would work, but if you want to use the gradient function rather than an image, you could simply place a view with a gradient fill behind a slightly smaller view that holds your content. As long as the smaller, front view is inset from the gradient view by the same distance on all sides then this will give the effect that you are looking for.
As per my suggestion use below code for achieve gradient color
self.layer.borderColor = UIColor(patternImage:UIImage(named: "myCorrectImgName.png")!).CGColor
Where "myCorrectImgName.png" is name of image that have gradient color or required image that you want.
Otherwise you will be find may examples from the Google for how to set gradient color.

Resources