Set the Gradient color in iOS swift
Solution:
The below code is used to set the gradient color to the particular view
first declare the colors
The below code is used to set the gradient color to the particular view
first declare the colors
let topColorsList: [String] = ["#f12711"]
let bottomColorsList: [String] = ["#f5af19"]
let gradientLayer:CAGradientLayer = CAGradientLayer()
gradientLayer.frame.size = cell.moreView.frame.size
gradientLayer.colors = [hexStringToUIColor(hex: topColorsList[indexPath.row]).cgColor,hexStringToUIColor(hex: bottomColorsList[indexPath.row]).cgColor]
//Use diffrent colors
cell.moreView.layer.addSublayer(gradientLayer)
The above code needs the below function for get the color from hexstring
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
Comments
Post a Comment