Swift - create array of custom colors - ios

Is there a way to create an array of custom colors? I know I can create custom colors in class UIColors but in my case I need an array of my custom colors.
I would like to achieve something like this:
let listColors: [UIColor] = [
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1),
]

You don't need a subclass to create an array do
let listColors = [
UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
]
access
listColors[index]
or
class Colors {
static let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
static let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
static let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
}
access
Colors.color1
or as global
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
access
color1
When you create a color divide by 255
UIColor(displayP3Red: 2/255.0, green: 2/255.0, blue: 2/255.0, alpha: 1)

Or if you want to be able to reference them by name or by array index:
let color1 = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), //White
let color2 = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0), //Gray
let color3 = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), //Red
let listColors: [UIColor] = [color1, color2, color3]

If you are supporting dark and light modes in your App, it is best to define your custom colors as assets in your App and then access them something like this
extension String {
static let blueColorName = "Blue"
static let brownColorName = "Brown"
static let crustaColorName = "Crusta"
static let darkRedColorName = "DarkRed"
static let defaultColorName = "Default"
static let flamingoColorName = "Tango"
static let khakiColorName = "Khaki"
static let greenColorName = "Green"
static let limeColorName = "Lime"
static let lustColorName = "Lust"
static let oliveColorName = "Olive"
static let orangeColorName = "Orange"
static let pinkColorName = "Pink"
static let purpleColorName = "Purple"
static let roseColorName = "Rose"
static let redColorName = "Red"
static let skyColorName = "Sky"
static let steelColorName = "Steel"
static let sunColorName = "Sun"
static let tealColorName = "Teal"
static let yellowColorName = "Yellow"
// Tile Background Colors
static let tileGrayColorName = "TileGray"
static let tileGreenColorName = "TileGreen"
static let tileRedColorName = "TileRed"
static let tileYellowColorName = "TileYellow"
}
extension UIColor {
// MARK: - Custom Color List For Color Picker
static let customColorsList: [(colorName: String, uiColor: UIColor)] = [
("Orange", UIColor(named: .orangeColorName)!),
("Tango", UIColor(named: .flamingoColorName)!),
("Crusta", UIColor(named: .crustaColorName)!),
("Yellow", UIColor(named: .yellowColorName)!),
("Sun", UIColor(named: .sunColorName)!),
("Khaki", UIColor(named: .khakiColorName)!),
("Lime", UIColor(named: .limeColorName)!),
("Green", UIColor(named: .greenColorName)!),
("Olive", UIColor(named: .oliveColorName)!),
("Teal", UIColor(named: .tealColorName)!),
("Sky", UIColor(named: .skyColorName)!),
("Blue", UIColor(named: .blueColorName)!),
("Steel", UIColor(named: .steelColorName)!),
("Pink", UIColor(named: .pinkColorName)!),
("Rose", UIColor(named: .roseColorName)!),
("Purple", UIColor(named: .purpleColorName)!),
("Lust", UIColor(named: .lustColorName)!),
("Dark Red", UIColor(named: .darkRedColorName)!),
("Brown", UIColor(named: .brownColorName)!),
("Default", UIColor(named: .defaultColorName)!)
]
}

Related

Color shading inside PieChart IOS

I need to create a color shading effect inside the pie chart for a particular slice only.The required image is as shown( need a light orange color inside the piechart corresponding to orange slice)
Iam using cocoapods charts library. This is the function:
let approvedColor: UIColor = UIColor(red: 19/255, green: 195/255, blue: 30/255, alpha: 1.0)
let pendingColor: UIColor = UIColor(red: 255/255, green: 148/255, blue: 56/255, alpha: 1.0)
let rejectedColor: UIColor = UIColor(red: 225/255, green: 74/255, blue: 74/255, alpha: 1.0)
let surveyData = [50,40,10]
func fillChart() {
var dataEntries = [PieChartDataEntry]()
for (val) in surveyData {
let entry = PieChartDataEntry(value: Double(val), label: nil)
dataEntries.append(entry)
}
let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
chartDataSet.colors = [approvedColor,pendingColor,rejectedColor]
chartDataSet.sliceSpace = 0
chartDataSet.selectionShift = 5
let chartData = PieChartData(dataSet: chartDataSet)
chartView.data = chartData
}

access Xcode color picker 'Pencils' option programmatically

When I choose my button's background color in the storyboard, I can use a colorpicker widget. One of the tabs is called 'pencils'.
This tab shows a picture of coloring pencils lined up. One pencil has the color 'mercury'.
Is there a way to access this predefined color through the UIColor object?
e.g.
let myColor = UIColor(pencils: "mercury")
You could access Mercury color in code this way:
let myColor = Color
Click on the white square, then click on others, finally you could select Mercury color in the pencil tab.
Using Fkzm's answer, here are all the colors.
public struct XCodePencilColorPalette {
private init() { }
public static let licorice = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
public static let lead = #colorLiteral(red: 0.1298420429, green: 0.1298461258, blue: 0.1298439503, alpha: 1)
public static let tungsten = #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1)
public static let iron = #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1)
public static let steel = #colorLiteral(red: 0.4756349325, green: 0.4756467342, blue: 0.4756404161, alpha: 1)
public static let tin = #colorLiteral(red: 0.5704585314, green: 0.5704723597, blue: 0.5704649091, alpha: 1)
public static let nickel = #colorLiteral(red: 0.5741485357, green: 0.5741624236, blue: 0.574154973, alpha: 1)
public static let aluminum = #colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1)
public static let magnesium = #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1)
public static let silver = #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1)
public static let mercury = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
public static let snow = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
public static let cayenne = #colorLiteral(red: 0.5807225108, green: 0.066734083, blue: 0, alpha: 1)
public static let mocha = #colorLiteral(red: 0.5787474513, green: 0.3215198815, blue: 0, alpha: 1)
public static let asparagus = #colorLiteral(red: 0.5738074183, green: 0.5655357838, blue: 0, alpha: 1)
public static let fern = #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1)
public static let clover = #colorLiteral(red: 0, green: 0.5603182912, blue: 0, alpha: 1)
public static let moss = #colorLiteral(red: 0, green: 0.5628422499, blue: 0.3188166618, alpha: 1)
public static let teal = #colorLiteral(red: 0, green: 0.5690457821, blue: 0.5746168494, alpha: 1)
public static let ocean = #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1)
public static let midnight = #colorLiteral(red: 0.004859850742, green: 0.09608627111, blue: 0.5749928951, alpha: 1)
public static let eggplant = #colorLiteral(red: 0.3236978054, green: 0.1063579395, blue: 0.574860394, alpha: 1)
public static let plum = #colorLiteral(red: 0.5810584426, green: 0.1285524964, blue: 0.5745313764, alpha: 1)
public static let maroon = #colorLiteral(red: 0.5808190107, green: 0.0884276256, blue: 0.3186392188, alpha: 1)
public static let maraschino = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
public static let tangerine = #colorLiteral(red: 1, green: 0.5781051517, blue: 0, alpha: 1)
public static let lemon = #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 1)
public static let lime = #colorLiteral(red: 0.5563425422, green: 0.9793455005, blue: 0, alpha: 1)
public static let spring = #colorLiteral(red: 0, green: 0.9768045545, blue: 0, alpha: 1)
public static let seaFoam = #colorLiteral(red: 0, green: 0.9810667634, blue: 0.5736914277, alpha: 1)
public static let turquoise = #colorLiteral(red: 0, green: 0.9914394021, blue: 1, alpha: 1)
public static let aqua = #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1)
public static let blueberry = #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1)
public static let grape = #colorLiteral(red: 0.5818830132, green: 0.2156915367, blue: 1, alpha: 1)
public static let magenta = #colorLiteral(red: 1, green: 0.2527923882, blue: 1, alpha: 1)
public static let strawberry = #colorLiteral(red: 1, green: 0.1857388616, blue: 0.5733950138, alpha: 1)
public static let salmon = #colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1)
public static let cantaloupe = #colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1)
public static let banana = #colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1)
public static let honeydew = #colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1)
public static let flora = #colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1)
public static let spindrift = #colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1)
public static let ice = #colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1)
public static let sky = #colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1)
public static let orchid = #colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1)
public static let lavender = #colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1)
public static let bubblegum = #colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1)
public static let carnation = #colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1)
}
I wanted to do the same thing, so I created a custom class. Hope this saves someone else some time :)
To access, call PencilColors.instance...
import Foundation
import UIKit
class PencilColors {
static let instance = PencilColors()
let pencilColors: [(color: UIColor, name: String, type: Int)] = [
//instead of creating an enum in each project:
//type 0 -> black and white
//type 1 -> dark colors
//type 2 -> medium colors
//type 3 -> light colors
(color: .black, name: "Black", type: 0),
(color: #colorLiteral(red: 0.1298420429, green: 0.1298461258, blue: 0.1298439503, alpha: 1), name: "Lead", type: 0),
(color: #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1), name: "Tungsten", type: 0),
(color: #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1), name: "Iron", type: 0),
(color: #colorLiteral(red: 0.4756349325, green: 0.4756467342, blue: 0.4756404161, alpha: 1), name: "Steel", type: 0),
(color: #colorLiteral(red: 0.5704585314, green: 0.5704723597, blue: 0.5704649091, alpha: 1), name: "Tin", type: 0),
(color: #colorLiteral(red: 0.5741485357, green: 0.5741624236, blue: 0.574154973, alpha: 1), name: "Nickel", type: 0),
(color: #colorLiteral(red: 0.6642242074, green: 0.6642400622, blue: 0.6642315388, alpha: 1), name: "Aluminum", type: 0),
(color: #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1), name: "Magnesium", type: 0),
(color: #colorLiteral(red: 0.8374180198, green: 0.8374378085, blue: 0.8374271393, alpha: 1), name: "Silver", type: 0),
(color: #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1), name: "Mercury", type: 0),
(color: .white, name: "White", type: 0),
(color: #colorLiteral(red: 0.5807225108, green: 0.066734083, blue: 0, alpha: 1), name: "Cayenne", type: 1),
(color: #colorLiteral(red: 0.5787474513, green: 0.3215198815, blue: 0, alpha: 1), name: "Mocha", type: 1),
(color: #colorLiteral(red: 0.5738074183, green: 0.5655357838, blue: 0, alpha: 1), name: "Asparagus", type: 1),
(color: #colorLiteral(red: 0.3084011078, green: 0.5618229508, blue: 0, alpha: 1), name: "Fern", type: 1),
(color: #colorLiteral(red: 0, green: 0.5603182912, blue: 0, alpha: 1), name: "Clover", type: 1),
(color: #colorLiteral(red: 0, green: 0.5628422499, blue: 0.3188166618, alpha: 1), name: "Moss", type: 1),
(color: #colorLiteral(red: 0, green: 0.5690457821, blue: 0.5746168494, alpha: 1), name: "Teal", type: 1),
(color: #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1), name: "Ocean", type: 1),
(color: #colorLiteral(red: 0.004859850742, green: 0.09608627111, blue: 0.5749928951, alpha: 1), name: "Midnight", type: 1),
(color: #colorLiteral(red: 0.3236978054, green: 0.1063579395, blue: 0.574860394, alpha: 1), name: "Eggplant", type: 1),
(color: #colorLiteral(red: 0.5810584426, green: 0.1285524964, blue: 0.5745313764, alpha: 1), name: "Plum", type: 1),
(color: #colorLiteral(red: 0.5808190107, green: 0.0884276256, blue: 0.3186392188, alpha: 1), name: "Maroon", type: 1),
(color: #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1), name: "Maraschino", type: 2),
(color: #colorLiteral(red: 1, green: 0.5781051517, blue: 0, alpha: 1), name: "Tangerine", type: 2),
(color: #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 1), name: "Lemon", type: 2),
(color: #colorLiteral(red: 0.5563425422, green: 0.9793455005, blue: 0, alpha: 1), name: "Lime", type: 2),
(color: #colorLiteral(red: 0, green: 0.9768045545, blue: 0, alpha: 1), name: "Spring", type: 2),
(color: #colorLiteral(red: 0, green: 0.9810667634, blue: 0.5736914277, alpha: 1), name: "Sea Foam", type: 2),
(color: #colorLiteral(red: 0, green: 0.9914394021, blue: 1, alpha: 1), name: "Turquoise", type: 2),
(color: #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1), name: "Aqua", type: 2),
(color: #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1), name: "Blueberry", type: 2),
(color: #colorLiteral(red: 0.5818830132, green: 0.2156915367, blue: 1, alpha: 1), name: "Grape", type: 2),
(color: #colorLiteral(red: 1, green: 0.2527923882, blue: 1, alpha: 1), name: "Magenta", type: 2),
(color: #colorLiteral(red: 1, green: 0.1857388616, blue: 0.5733950138, alpha: 1), name: "Strawberry", type: 2),
(color: #colorLiteral(red: 1, green: 0.4932718873, blue: 0.4739984274, alpha: 1), name: "Salmon", type: 3),
(color: #colorLiteral(red: 1, green: 0.8323456645, blue: 0.4732058644, alpha: 1), name: "Cantaloupe", type: 3),
(color: #colorLiteral(red: 0.9995340705, green: 0.988355577, blue: 0.4726552367, alpha: 1), name: "Banana", type: 3),
(color: #colorLiteral(red: 0.8321695924, green: 0.985483706, blue: 0.4733308554, alpha: 1), name: "Honeydew", type: 3),
(color: #colorLiteral(red: 0.4500938654, green: 0.9813225865, blue: 0.4743030667, alpha: 1), name: "Flora", type: 3),
(color: #colorLiteral(red: 0.4508578777, green: 0.9882974029, blue: 0.8376303315, alpha: 1), name: "Sprindrift", type: 3),
(color: #colorLiteral(red: 0.4513868093, green: 0.9930960536, blue: 1, alpha: 1), name: "Ice", type: 3),
(color: #colorLiteral(red: 0.4620226622, green: 0.8382837176, blue: 1, alpha: 1), name: "Sky", type: 3),
(color: #colorLiteral(red: 0.476841867, green: 0.5048075914, blue: 1, alpha: 1), name: "Orchid", type: 3),
(color: #colorLiteral(red: 0.8446564078, green: 0.5145705342, blue: 1, alpha: 1), name: "Lavender", type: 3),
(color: #colorLiteral(red: 1, green: 0.5212053061, blue: 1, alpha: 1), name: "Bubblegum", type: 3),
(color: #colorLiteral(red: 1, green: 0.5409764051, blue: 0.8473142982, alpha: 1), name: "Carnation", type: 3),
]
func getAllColors() -> [UIColor] {
let colorArray = self.pencilColors.map { (existingType) -> UIColor in
return existingType.color
}
return colorArray
}
func getBlackAndWhiteColors() -> [UIColor] {
let colorArray = self.pencilColors.filter { (existingColor) -> Bool in
return existingColor.type == 0
}.map { (currentType) -> UIColor in
return currentType.color
}
return colorArray
}
func getDarkColors() -> [UIColor] {
let colorArray = self.pencilColors.filter { (existingColor) -> Bool in
return existingColor.type == 1
}.map { (currentType) -> UIColor in
return currentType.color
}
return colorArray
}
func getMediumColors() -> [UIColor] {
let colorArray = self.pencilColors.filter { (existingColor) -> Bool in
return existingColor.type == 2
}.map { (currentType) -> UIColor in
return currentType.color
}
return colorArray
}
func getLightColors() -> [UIColor] {
let colorArray = self.pencilColors.filter { (existingColor) -> Bool in
return existingColor.type == 3
}.map { (currentType) -> UIColor in
return currentType.color
}
return colorArray
}
func getColorName(fromColor color: UIColor) -> String {
let name = self.pencilColors.first { (existingColor) -> Bool in
return existingColor.color == color
}.map { (existingType) -> String in
return existingType.name
}
return name ?? ""
}
}

Gradient color as a Background color of UIButton in Swift4

I have a button and i'm trying to set its background color gradient.I have three hex values which i converted to RGB and written three values in my code and pass all the three values to the button frame. But when i run the app button background not changes according to the color i have set in the code. My code to get three color codes is this,
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183, green: 46, blue: 79, alpha: 1)
let color2 = UIColor(red: 232, green: 79, blue: 80, alpha: 1)
let color3 = UIColor(red: 145, green: 21, blue: 79, alpha: 1)
gradientColor.colors = [color1,color2,color3]
self.loginButton.layer.addSublayer(gradientColor)
Try this and see:
#IBOutlet weak var loginButton: UIButton!
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
gradientColor.colors = [UIColor.blue.cgColor,UIColor.red.withAlphaComponent(1).cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}
Here is result:
Also note: Values for RGB colors in your code, are much higher than its normal value. See your console log, it might printed following error:
[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
Divide all values with 255.0 and use insertSubLayer function.
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
Here is result with your color code values:
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
gradientColor.colors = [color1.cgColor,color2.cgColor,color3.cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}

App crashes on accessing first static variable in class

I'm building an iPhone app using Swift. I've created a Settings class and declared some static variables in them, for storing colors. However, whenever I try to access the first variable I've declared (such as Settings.grayBorderColor below), the app crashes (with some message about Settings.grayBorderColor.unsafeMutableAddressor). I can access any properties below the first one just fine, and if I switch the order of the properties/variables, it is still accessing whichever property is declared first that causes the crash.
class Settings {
// MARK: Properties
static let grayBorderColor = UIColor(red: 0.76, green: 0.76, blue: 0.76, alpha: 1.0)
static let lightGreenColor = UIColor(red: 0.66, green: 1.0, blue: 0.66, alpha: 1.0)
static let darkGreenColor = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
static let darkRedColor = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
static let lightRedColor = UIColor(red: 1, green: 0.66, blue: 0.66, alpha: 1.0)
static let lightGrayColor = UIColor.lightGrayColor()
static let mediumGrayColor = UIColor.darkGrayColor()
}
What am I doing wrong?
Change your code to this:
class Settings {
// MARK: Properties
static let grayBorderColor: UIColor! = UIColor(red: 0.76, green: 0.76, blue: 0.76, alpha: 1.0)
static let lightGreenColor: UIColor! = UIColor(red: 0.66, green: 1.0, blue: 0.66, alpha: 1.0)
static let darkGreenColor: UIColor! = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
static let darkRedColor: UIColor! = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
static let lightRedColor: UIColor! = UIColor(red: 1, green: 0.66, blue: 0.66, alpha: 1.0)
static let lightGrayColor: UIColor! = UIColor.lightGrayColor()
static let mediumGrayColor: UIColor! = UIColor.darkGrayColor()
}
You were missing property types. As stated in the documentation, calculated properties should have type specifier.

Randomize color in XCode

I'm trying to randomize the text color of a label in XCode with some colors I already created, I've tried different ways and this is the closest I got.
override func viewDidLoad() {
super.viewDidLoad()
let color1 = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 80/255, green: 1, blue: 200/255, alpha: 1)
let color3 = UIColor(red: 150/255, green: 80/255, blue: 1, alpha: 1)
let color4 = UIColor(red: 1, green: 80/255, blue: 80/255, alpha: 1)
let color5 = UIColor(red: 80/255, green: 1, blue: 80/255, alpha: 1)
var randomEight = arc4random_uniform(5)+1
var randomColor:String = String(format:"color%i", randomEight)
randomLabel.textColor = randomColor
}
However, the randomColor variable is a String and I can't transform it to a UIColor.
Put all of the colors in an array, then use randomEight as the index in the array to get the right color. Also, you may want to check the integer division to make sure you get the right colors.
let color1 = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 80/255, green: 1, blue: 200/255, alpha: 1)
let color3 = UIColor(red: 150/255, green: 80/255, blue: 1, alpha: 1)
let color4 = UIColor(red: 1, green: 80/255, blue: 80/255, alpha: 1)
let color5 = UIColor(red: 80/255, green: 1, blue: 80/255, alpha: 1)
let colors = [color1, color2, color3, color4, color5]
let randomEight = Int(arc4random_uniform(UInt32(colors.count)))
randomLabel.textColor = colors[randomEight]
Put the colours in an array, then select a random index, e.g.:
let colors: [UIColor] = [ UIColor.redColor(),
UIColor.blueColor(),
UIColor.greenColor()
] // ^- replace with your own colors
let randomColor = colors[Int(arc4random_uniform(UInt32(colors.count)))]
Note the necessary casts to work with arc4random_uniform. Also note that the indices start at 0, so do not add 1 to the random number.
There is no need for the randomEight (why is it “eight” anyhow?), and if the single text colour is the only place you use the colour, you can even leave out the randomColor and assign directly to randomLabel.textColor.
let randomEight = arc4random_uniform(5)+1
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
if randomEight == 1 {
randomLabel.textColor = UIColor.cyanColor()
}
else if randomEight == 2 {
randomLabel.textColor = UIColor.purpleColor()
}
else if randomEight == 3{
randomLabel.textColor = UIColor.orangeColor()
} else if randomEight == 4 {
randomLabel.textColor = UIColor.blueColor()
}
else {
randomLabel.textColor = UIColor.greenColor()
}
return randomLabel
}
Try this oneliner ;)
randomLabel.textColor = UIColor(red : CGFloat(arc4random_uniform(255)) , green : CGFloat(arc4random_uniform(255)), blue : CGFloat(arc4random_uniform(255)) , alpha : 1)

Resources