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)
Related
Based on response data trying to set UILabel background and text color.
From my below code its only setting when I am scrolling tableview.
When immediately loaded response data Need to set UILabel background & Text Color
func setBackGroundTextColors(jobType: String) -> (UIColor,UIColor) {
guard stringValue == "YES" else {
return ( #colorLiteral(red: 0.6784313725, green: 0.9529411765, blue: 0.9882352941, alpha: 1) , #colorLiteral(red: 0.5058823529, green: 0.4549019608, blue: 0.8980392157, alpha: 1) )
}
return ( #colorLiteral(red: 1, green: 0.9490196078, blue: 0.8941176471, alpha: 1) , #colorLiteral(red: 1, green: 0.4901960784, blue: 0, alpha: 1) )
}
CellforRowAtIndexPath
let trueValue = data[indexPath.row].trueValue
cell.lbl_Name?.backgroundColor = self.setBackGroundTextColors(jobType: trueValue).0
cell.lbl_Name?.textColor = self.setBackGroundTextColors(jobType: trueValue).1
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)!)
]
}
I need to define colors for graph based on the values(example : green for values between 0-50 and red for values between 50-100).I tried with gradient colors but it is not as expected. Could anyone help me out in this please? I'm using Swift 3.
I need the bottom of the graph to be green and upper part as red
Declare out of class for this code
class Colors {
var gl:CAGradientLayer!
init() {
let colorTop = UIColor(red: 135.0/255.0, green: 155.0/255.0, blue: 36.0/255.0, alpha: 1.0).cgColor
let colorcenter = UIColor(red: 186.0/255.0, green: 213.0/255.0, blue: 49.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 224.0/255.0, green: 241.0/255.0, blue: 142.0/255.0, alpha: 1.0).cgColor
self.gl = CAGradientLayer()
self.gl.colors = [colorTop, colorcenter, colorBottom]
self.gl.locations = [0.5,0.8, 1.0]
}
}
declate in viewDidLoad Method.
let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)
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)
}
I'm trying to modify the colour of a UIButton in Swift using the code below:
struct ColorWheel {
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
func randomColor() -> UIColor {
var unsignedArrayCount = UInt32(colorsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return colorsArray[randomNumber]
}
}
var colorWheel = ColorWheel()
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
myButton.setTitleColor(randomColor, forState: .Normal)
This code will work perfectly when changing the background colour, but when I try to use the setTitleColor method, I get the following error:
(UIButton) -> 0' does not have a member named 'setTitleColor'