The method for changing the String color "rgba(255, 255, 255, 1)" to a Swift color is:
Here's the Swift code to accomplish this:
let colorString = "rgba(255, 255, 255, 1)"
let componentsString = colorString.replacingOccurrences(of: "rgba(", with: "").replacingOccurrences(of: ")", with: "")
let componentsArray = componentsString.components(separatedBy: ",").map { CGFloat(Float($0.trimmingCharacters(in: .whitespacesAndNewlines))!/255.0) }
let color = UIColor(red: componentsArray[0], green: componentsArray[1], blue: componentsArray[2], alpha: componentsArray[3])
The resulting color
object will be a Swift UIColor with the same RGBA values as the original string.
Asked: 2021-08-31 11:00:00 +0000
Seen: 10 times
Last updated: Sep 12 '22