Changing UIButton border colours using layer - ios

I have a UIButton, I have put a border on the top and bottom of the UILabel. While changing the colour of the line, below I have set the line to white. I can change the line to a predetermined colour such as green using UIColor.green.cgColor.
The problem is I want to set the colour to topBorder.strokeColor = UIColor.init(red: 50, green: 50, blue: 50, alpha: 0.5).cgColor.
When I do this the colour still comes out white. Why does this happen? I can set it to a predetermined colour but it can set it to the specific colour that I want.
let topBorderTerms = CAShapeLayer()
let topPathTerms = UIBezierPath()
topPathTerms.move(to: CGPoint(x: 0, y: 0))
topPathTerms.addLine(to: CGPoint(x: Terms.frame.width, y: 0))
topBorderTerms.path = topPath.cgPath
topBorderTerms.strokeColor = UIColor.white.cgColor
topBorderTerms.lineWidth = 1.0
topBorderTerms.fillColor = UIColor.white.cgColor
Terms.layer.addSublayer(topBorderTerms)

Replace
topBorder.strokeColor = UIColor.init(red: 50, green: 50, blue: 50, alpha: 0.5).cgColor.
with
topBorder.strokeColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 0.5).cgColor
You miss divide by 255

Related

UIBezierPath color change custom

I need to change the color of the UIBezierPath, but if I set a custom color, then there is simply no color.
UIColor(red: 255/255.0, green: 249/255.0, blue: 244/255.0, alpha: 1.0)
Tried adding colors via extension, but there is no color either.
UIColor.themeColor.setFill()
extension UIColor {
class var themeColor: UIColor {
return UIColor(red: 255/255.0, green: 249/255.0, blue: 244/255.0, alpha: 1.0)
}
}
Standard colors like .blue, .green, .red - work, but I need to understand how to set my own color through rgb
Here is my code
class MyView: UIView {
override func draw(_ rect: CGRect) {
let path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 303.19, height: 495.93))
path.stroke()
}
}
let views = MyView(frame: CGRect(x: 36.62, y: 77.54, width: 303.19, height: 495.93))
views.backgroundColor = .clear
views.rotate(radians: -45.84)
view.addSubview(views)
Use this if you want to change the color in UIBezierPath
path.fillColor = UIColor(red: 255/255.0, green: 249/255.0, blue: 244/255.0, alpha: 1.0).cgColor
path.fillColor = UIColor.red.cgColor
path.fillColor = UIColor(red: 155/255.0, green: 149/255.0, blue: 234/255.0, alpha: 1.0).cgColor
Works well

TextView borderColor Not Changing

I am trying to change the borderColor of a textView in Swift. If I set it using a preset color it works fine:
inView.layer.borderColor = UIColor.green.cgColor
However, when I try to set it to a custom RGB value, the borderColor does not change:
inView.layer.borderColor = UIColor.init(red: 100, green: 230, blue: 100, alpha: 1).cgColor
You need to construct color properly ($value/255.0) as color range is from 0 to 1 ( aka 0 to 255/255 )
inView.layer.borderColor = UIColor(red: 100/255.0, green: 230/255.0, blue: 100/255.0, alpha: 1).cgColor

Apply CAGradientLayer to layer.borderColor

I'm trying to implement a underlined UITextField with a gradient. Therefore I created a extension with a function underlined().
To get a gradient, I created a CAGradientLayer and made these customizations:
func underlined(){
let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1).cgColor
let sndColor = UIColor(red: 106/255, green: 178/255, blue: 255/255, alpha: 1).cgColor
let gradient: CAGradientLayer = CAGradientLayer()
gradient.colors = [color, sndColor]
gradient.locations = [0.0, 1.0]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
let width = CGFloat(2.0)
gradient.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
gradient.borderWidth = width
self.layer.insertSublayer(gradient, at: 0)
self.layer.masksToBounds = true
}
A underline is being displayed, but it solely solid black - I've tried to change the colors, but it remains black (Issue outdated - see edit).
Does anybody see the issue?
Edit:
Adding gradient.borderColor = UIColor.blue.cgColor let me change the color of the line - but how can I apply the gradient on the border color?
Remove the below line from your code:
gradient.borderWidth = width
Screenshot:
Your code was not working because, the borderWidth is covering whole of the gradient frame.Try setting the borderColor, then you'll see the difference.
gradient.borderColor = UIColor.red.cgColor
Let me know if you still face any issues.

My label do not show after the layer added in the self.view but View Hierarchy show

I drag my labels to my self.view in storyboard, you can see the white labels, in the gray vc, I am sorry for the tiny distinction to found :
Then I add a gradual layer use code :
let fromColor = UIColor.init(red: 243/255.0, green: 143/255.0, blue: 30/255.0, alpha: 1).cgColor
let centerColor = UIColor.init(red: 237/255.0, green: 90/255.0, blue: 36/255.0, alpha: 1).cgColor
let toColor = UIColor.init(red: 233/255.0, green: 28.0/255.0, blue: 36/255.0, alpha: 1).cgColor
gradul_layer = CAGradientLayer.init()
gradul_layer?.frame = CGRect.init(x: 0, y: 64, width: initFrame.width, height: initFrame.height)
gradul_layer?.colors = [
fromColor,
centerColor,
toColor
]
gradul_layer?.startPoint = CGPoint.init(x: 0.5, y: 0.3)
gradul_layer?.endPoint = CGPoint.init(x: 0.5, y: 0.7)
self.view.layer.addSublayer(gradul_layer!)
And then when I run my app in simulator (or device) can not show the label, but I can capture the View Hierarchy in Xcode, and I can see the label.
You see the picture, left is simulator(the labels did not show), right is the capture View Hierarchy(the labels shows up).
Update
My question is not how to let the label show in my screen, my doubt is why View Hierarchy show, my device or simulator will not.

Swift: Adding gradient layer to button. Layer length error

let gradient: CAGradientLayer = CAGradientLayer()
let colorTop = UIColor(red: 112.0/255.0, green: 219.0/255.0, blue: 155.0/255.0, alpha: 1.0).CGColor
let colorBottom = UIColor(red: 86.0/255.0, green: 197.0/255.0, blue: 238.0/255.0, alpha: 1.0).CGColor
gradient.colors = [colorTop, colorBottom]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = loginButton.bounds
gradient.cornerRadius = 5
loginButton.layer.addSublayer(gradient)
The resulting gradient runs off goes beyond the button's frame. Why does this happen?
Probably you are setting gradient layer in viewDidLoad() or viewWillAppear(). At that time controller did not calculated views sizes. At time you add this sublayer button size was not calculated yet, so sublayer size is wrong. So you should fix next things:
First of all you should add gradient at viewDidAppear(), at this point all view's sizes are calculated.
Second, you should use layer.insertSublayer(layer, atIndex:index) instead of addSublayer(layer). Because in your case sublayer will hide buttons native layer (title, background...)
You should recalculate your sublayer size in viewDidLayoutSubviews(). Because when your button will change it's size (while rotating for example), sublayer will not change it's frame, so you should change it by yourself.
Add clipsToBounds and cornerRadius to loginbutton. That should fix the problem.
loginButton.clipsToBounds = true
loginButton.layer.cornerRadius = 5;
Is your Login Button's frame correct?It seems correct when I reproduce
let loginButton = UIButton(frame: CGRect(x: 10, y: 50, width: 300, height: 30))
self.view.addSubview(loginButton)
let gradient:CAGradientLayer = CAGradientLayer()
let colorTop = UIColor(red: 112.0/255.0, green: 219.0/255.0, blue: 155.0/255.0, alpha: 1.0).CGColor
let colorBottom = UIColor(red: 86.0/255.0, green: 197.0/255.0, blue: 238.0/255.0, alpha: 1.0).CGColor
gradient.colors = [colorTop, colorBottom]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = loginButton.bounds
gradient.cornerRadius = 5
loginButton.layer.addSublayer(gradient)
And it appear like below

Resources