UIBezierPath color change custom - ios

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

Related

Changing UIButton border colours using layer

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

How can I make a collectionViewCell like the one on Apple's app Shortcuts?

I want to make a collectionViewCell like the one on Apple's app Shortcuts, especially with the random background colors.
Shortcuts app UI:
First of all I'm sorry if my question wasn't clear, however I think solved my problem. I wanted to create a similar collectionViewCell to the one on the Shortcuts app and I think I did pretty well, here's what I've done...
Final result
I put the colors I wanted into a Hash Table and then generated a random key to get the pictures array and then created an extension to the UIView class to make the gradient and passed the index of the colors array to the gradient function from my cellRandomBackgroundColors(), here's my collectionViewCell class and the extension if anyone want it.
import UIKit
class MainCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK:- UI
let iconImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "Folder")
imageView.contentMode = .scaleAspectFit
return imageView
}()
let checklistNameLabel: UILabel = {
let label = UILabel()
label.text = "List"
label.textColor = UIColor.white
label.font = UIFont.boldSystemFont(ofSize: 19)
label.textAlignment = .left
return label
}()
let remainingsLabel: UILabel = {
let label = UILabel()
label.text = "2 Remaining"
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 13)
label.textAlignment = .left
return label
}()
let editButton: UIButton = {
let button = UIButton(type: UIButton.ButtonType.custom) as UIButton
button.setImage(UIImage(named: "More"), for: UIControl.State.normal)
return button
}()
//MARK:- Setup Cell
func setupCell() {
roundCorner()
gradientBackgroundColor()
setCellShadow()
self.addSubview(iconImageView)
self.addSubview(checklistNameLabel)
self.addSubview(remainingsLabel)
self.addSubview(editButton)
iconImageView.anchor(top: safeTopAnchor, left: safeLeftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 36, height: 36)
checklistNameLabel.anchor(top: iconImageView.bottomAnchor, left: safeLeftAnchor, bottom: nil, right: nil, paddingTop: 18, paddingLeft: 8, paddingBottom: 0, paddingRight: 0)
remainingsLabel.anchor(top: checklistNameLabel.bottomAnchor, left: safeLeftAnchor, bottom: nil, right: nil, paddingTop: 4, paddingLeft: 8, paddingBottom: 0, paddingRight: 0)
editButton.anchor(top: safeTopAnchor, left: nil, bottom: nil, right: safeRightAnchor, paddingTop: 8, paddingLeft: 0, paddingBottom: 0, paddingRight: 8, width: 36, height: 36)
}
//MARK:- Methods
func cellRandomBackgroundColors() -> [UIColor] {
//Colors
let red = [#colorLiteral(red: 0.9654200673, green: 0.1590853035, blue: 0.2688751221, alpha: 1),#colorLiteral(red: 0.7559037805, green: 0.1139892414, blue: 0.1577021778, alpha: 1)]
let orangeRed = [#colorLiteral(red: 0.9338900447, green: 0.4315618277, blue: 0.2564975619, alpha: 1),#colorLiteral(red: 0.8518816233, green: 0.1738803983, blue: 0.01849062555, alpha: 1)]
let orange = [#colorLiteral(red: 0.9953531623, green: 0.54947716, blue: 0.1281470656, alpha: 1),#colorLiteral(red: 0.9409626126, green: 0.7209432721, blue: 0.1315650344, alpha: 1)]
let yellow = [#colorLiteral(red: 0.9409626126, green: 0.7209432721, blue: 0.1315650344, alpha: 1),#colorLiteral(red: 0.8931249976, green: 0.5340107679, blue: 0.08877573162, alpha: 1)]
let green = [#colorLiteral(red: 0.3796315193, green: 0.7958304286, blue: 0.2592983842, alpha: 1),#colorLiteral(red: 0.2060100436, green: 0.6006633639, blue: 0.09944178909, alpha: 1)]
let greenBlue = [#colorLiteral(red: 0.2761503458, green: 0.824685812, blue: 0.7065336704, alpha: 1),#colorLiteral(red: 0, green: 0.6422213912, blue: 0.568986237, alpha: 1)]
let kindaBlue = [#colorLiteral(red: 0.2494148612, green: 0.8105323911, blue: 0.8425348401, alpha: 1),#colorLiteral(red: 0, green: 0.6073564887, blue: 0.7661359906, alpha: 1)]
let skyBlue = [#colorLiteral(red: 0.3045541644, green: 0.6749247313, blue: 0.9517192245, alpha: 1),#colorLiteral(red: 0.008423916064, green: 0.4699558616, blue: 0.882807076, alpha: 1)]
let blue = [#colorLiteral(red: 0.1774400771, green: 0.466574192, blue: 0.8732826114, alpha: 1),#colorLiteral(red: 0.00491155684, green: 0.287129879, blue: 0.7411141396, alpha: 1)]
let bluePurple = [#colorLiteral(red: 0.4613699913, green: 0.3118675947, blue: 0.8906354308, alpha: 1),#colorLiteral(red: 0.3018293083, green: 0.1458326578, blue: 0.7334778905, alpha: 1)]
let purple = [#colorLiteral(red: 0.7080290914, green: 0.3073516488, blue: 0.8653779626, alpha: 1),#colorLiteral(red: 0.5031493902, green: 0.1100070402, blue: 0.6790940762, alpha: 1)]
let pink = [#colorLiteral(red: 0.9495453238, green: 0.4185881019, blue: 0.6859942079, alpha: 1),#colorLiteral(red: 0.8123683333, green: 0.1657164991, blue: 0.5003474355, alpha: 1)]
let colorsTable: [Int: [UIColor]] = [0: red, 1: orangeRed, 2: orange, 3: yellow, 4: green, 5: greenBlue, 6: kindaBlue, 7: skyBlue, 8: blue, 9: bluePurple, 10: bluePurple, 11: purple, 12: pink]
let randomColors = colorsTable.values.randomElement()
return randomColors!
}
func gradientBackgroundColor() {
let colors = cellRandomBackgroundColors()
self.contentView.setGradientBackgroundColor(colorOne: colors[0], colorTow: colors[1])
}
func roundCorner() {
self.contentView.layer.cornerRadius = 12.0
self.contentView.layer.masksToBounds = true
self.contentView.layer.borderWidth = 1.0
self.contentView.layer.borderColor = UIColor.clear.cgColor
}
}
The extension to create the gradients.
import UIKit
extension UIView {
func setGradientBackgroundColor(colorOne: UIColor, colorTow: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [colorOne.cgColor, colorTow.cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
layer.insertSublayer(gradientLayer, at: 0)
}
}

Change Tab Bar background transparency

I am trying to make my Tab Bar in my tab bar controller to have a low opacity background, so it is semi transparent, i am trying to do this programatically, however the background changes to the correct color, but always appears solid, with no transparency.
Here is the code in my TabBarViewController
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.unselectedItemTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.4)
self.tabBar.barTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.5)
// Do any additional setup after loading the view.
}
}
For such a case, you should generate a customized UIImage to the tab bar backgroundimage property.
Let's assume that your desired color for your tab bar -that should be transparent- is black, you could implement in your custom UITabBarController:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let transperentBlackColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.5)
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
transperentBlackColor.setFill()
UIRectFill(rect)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
tabBar.backgroundImage = image
}
UIGraphicsEndImageContext()
}
}
So, as an Output, if we assumed that the background color of your view controller is blue, it should looks like:
As shown, the tab bar is not purely black, it contains transparency (0.5) as expected.
Also, for checking how you could generate a solid-color UIImage, you could check this SO answer : Create UIImage with solid color in Swift.
Just replace barTintColor with backgroundColor.
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.unselectedItemTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.4)
self.tabBar.backgroundColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.5)
// Do any additional setup after loading the view.
}
private func setTabbarOpacity() {
let transperentBlackColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.75)
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
transperentBlackColor.setFill()
UIRectFill(rect)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
tabBar.backgroundImage = image
}
UIGraphicsEndImageContext()
}

Swift setting a custom font color for a SKLabelNode

I'm trying to set a custom color to a SKLabelNode.
Here is my code:
coinLabel.fontName = "Bebas"
coinLabel.fontSize = 30
coinLabel.fontColor = SKColor(red: 233, green: 157, blue: 20, alpha: 1)
coinLabel.text = " \(UserDefaults().integer(forKey: "COINSCORE"))"
coinLabel.position = CGPoint(x: -self.frame.width / 3.1, y: self.frame.height / 2.23)
coinLabel.zPosition = 1.1
self.addChild(coinLabel)
However this is not working and shows the previous color I set which was simply by doing:
coinLabel.fontColor = .black
you need to use something like
SKColor(red: 233/255, green: 157/255, blue: 20/255, alpha: 1)
because red, green and blue is a value between 0.0 and 1.0
If your like me and can never remember or want to type in ALL those 255's ;). You can extend SKColor, so that you can enter the rgb's just using the values.
extension SKColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha:a)
}
convenience init(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat) {
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha:1)
}
}
now you can just type
coinLabel.fontColor = SKColor(red: 233, green: 157, blue: 20, alpha: 1)

How to change background color of UITextField to custom gradient color in iOS using Swift

I have done gradient coloring before for changing color of my parent view now I am using same method to change the background color of UITextField but its not working.
This is the method I used for my view:
func setGradientBackgroundOfView()
{
// assigning gradient color to background View
let endingColorOFGradient = UIColor(colorLiteralRed: 133/255, green: 210/255, blue: 230/255, alpha: 1.0).CGColor
let startingColorOfGradient = UIColor(colorLiteralRed: 50/255, green: 189/255, blue: 170/255, alpha: 1.0).CGColor
gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
gradient.endPoint = CGPoint(x: 0.5, y: 1.0)
gradient.colors = [startingColorOfGradient , endingColorOFGradient]
self.view.layer.insertSublayer(gradient, atIndex: 0)
}
And this method for setting layer:
override func viewWillLayoutSubviews() {
gradient.frame = self.view.bounds
}
I am calling my setGradientBackgroundOfView() in viewDidLoad() and it's working perfect but if I change
self.view.layer.insertSublayer(gradient, atIndex: 0)
with
self.textFieldUserName.layer.insertSublayer(gradient, atIndex: 0)
it does not work. What am I missing here?

Resources