I'm customizing my UIButton using MDCButton. I want to make my button outlined and customize its color.
In this case, I'm using MDCOutlinedButtonThemer.
I also implement MDCButton (MDCButtonColorThemer) with custom color in other button and it working.
I've tried to set my button with default MDCOutlinedButton and it works.
This is my code :
MDCOutlinedButtonThemer.applyScheme(buttonScheme, to: self.btnAddToCart)
MDCButtonColorThemer.applySemanticColorScheme(ApplicationScheme.shared.colorScheme, to: self.btnBuy)
This is the ApplicationScheme.swift :
public let colorScheme: MDCColorScheming = {
let scheme = MDCSemanticColorScheme(defaults: .material201804)
//TODO: Customize our app Colors after this line
scheme.primaryColor = UIColor(red: 255.0 / 255.0, green: 201.0 / 255.0, blue: 46.0 / 255.0, alpha: 1)
//scheme.primaryColorVariant = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onPrimaryColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.secondaryColor = UIColor(red: 254.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onSecondaryColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.surfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onSurfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.backgroundColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onBackgroundColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.errorColor = UIColor(red: 197.0/255.0, green: 3.0/255.0, blue: 43.0/255.0, alpha: 1.0)
return scheme
}()
I want to make "Add to cart" button's border color same with "buy" button's color
You can use next color settings for creation MDCButton:
let myButton = MDCButton()
myButton.setTitle("Add to cart", for: .normal)
myButton.isUppercaseTitle = false
myButton.addTarget(self, action: #selector(myButtonAction(_:)), for: .touchUpInside)
myButton.frame = CGRect(x: 10, y: 10, width: 150, height: 40)
myButton.setBackgroundColor(UIColor.white)
myButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
myButton.setBorderColor(UIColor.lightGray, for: UIControl.State.normal)
myButton.setBorderWidth(1.0, for: UIControl.State.normal)
myButton.layer.cornerRadius = 5
view.addSubview(myButton)
Related
I have several colors defined in my SKScene:
let color1 = UIColor(red: 1, green: 153/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 74/255, green: 134/255, blue: 232/255, alpha: 1)
let color3 = UIColor(red: 0, green: 0, blue: 1, alpha: 1)
let color4 = UIColor(red: 0, green: 1, blue: 0, alpha: 1)
let color5 = UIColor(red: 153/255, green: 0, blue: 1, alpha: 1)
let color6 = UIColor(red: 1, green: 0, blue: 0 , alpha: 1)
These colors correspond to tiles with different values in my game. The one tile goes with color1 and so on...
When the tiles are added together, I add their values and want to give them a new color according to their value.
So I want the value of the tile to play an effect on what color the tile is.
I have tried:
tile.color = UIColor(named: "color\(value)") ?? color1
But when I do this, it always uses the default value(color1).
How do I make so the value of the tile plays an effect in the color of the tile?
Named UIColors are initalized from color set in xcassets catalog.
You can set color depending on your value
switch value {
case 2: tile.color = color2
...
default: tile.color = color1
}
or you can create color sets in xcassets catalog.
UIColor named: only works when you define a color set asset. Since your colors are defined in code, UIColor named: will never return anything but nil.
One solution is to put your colors into a dictionary instead of separate variables.
let colors: [String: UIColor] = [
"color1" : UIColor(red: 1, green: 153/255, blue: 0, alpha: 1),
"color2" : UIColor(red: 74/255, green: 134/255, blue: 232/255, alpha: 1),
"color3" : UIColor(red: 0, green: 0, blue: 1, alpha: 1),
"color4" : UIColor(red: 0, green: 1, blue: 0, alpha: 1),
"color5" : UIColor(red: 153/255, green: 0, blue: 1, alpha: 1),
"color6" : UIColor(red: 1, green: 0, blue: 0 , alpha: 1),
]
Then you can get your color as:
tile.color = colors["color\(value)"] ?? colors["color1"]!
This question already has answers here:
UIColor not working with RGBA values
(6 answers)
Closed 3 years ago.
Having failed miserably at further attempts to solve this question on my own, I'm trying something I thought would work for certain:
func switchColor(data:UInt32){
switch data {
case 1..<200:
backgroundGeometry.firstMaterial?.diffuse.contents =
UIColor(red: CGFloat(242), green: CGFloat(90), blue: CGFloat(90), alpha: 1.0)
case 200..<400:
backgroundGeometry.firstMaterial?.diffuse.contents =
UIColor(red: CGFloat(252), green: CGFloat(162), blue: CGFloat(115), alpha: 1.0)
case 400..<600:
backgroundGeometry.firstMaterial?.diffuse.contents =
UIColor(red: CGFloat(244), green: CGFloat(235), blue: CGFloat(99), alpha: 1.0)
case 600..<800:
backgroundGeometry.firstMaterial?.diffuse.contents =
UIColor(red: CGFloat(110), green: CGFloat(195), blue: CGFloat(175), alpha: 1.0)
case 800..<1000:
backgroundGeometry.firstMaterial?.diffuse.contents =
UIColor(red: CGFloat(91), green: CGFloat(118), blue: CGFloat(211), alpha: 1.0)
default:
backgroundGeometry.firstMaterial?.diffuse.contents = UIColor.green
}
}
All the non-default cases turns the node white.
The default case does turn it green - and within each case, statements like UIColor.red, UIColor.blue work fine as well.
So why the heck doesn't the above statements work?
Hope you can help, I'm completely at a loss here :(
Edit: Thanks for the swift and not least correct answers! All accepted and upvoted, but I'm too much of a newbie for it to display. Thanks! :)
This should work for you:
func switchColor(data: UInt32) {
guard let contents = backgroundGeometry.firstMaterial?.diffuse.contents else {
fatalError("First material is nil") // If this really can be empty, just replace this with return
}
switch data {
case 1..<200:
contents = UIColor(red: 242/255, green: 90/255, blue: 90/255, alpha: 1)
case 200..<400:
contents = UIColor(red: 252/255, green: 162/255, blue: 115/255, alpha: 1)
case 400..<600:
contents = UIColor(red: 244/255, green: 235/255, blue: 99/255, alpha: 1)
case 600..<800:
contents = UIColor(red: 110/255, green: 195/255, blue: 175/255, alpha: 1)
case 800..<1000:
contents = UIColor(red: 91/255, green: 118/255, blue: 211/255, alpha: 1)
default:
contents = .green
}
}
The maximum value of a color is 1.0, not 255. Therefore you need to divide the values.
According to the documentation, the red, green, blue and alfa values are Float between 0.0 to 1.0 respectively. Also, a value below 0.0 is treated as 0.0 and value above 1.0 is treated as 1.0.
So you must construct the UIColor as this
UIColor(red: 91/255, green: 118/255, blue: 211/255, alpha: 1)
You need to construct them like
UIColor(red:242.0/255.0, green:90.0/255.0, blue:90.0/255.0, alpha: 1.0)
you can find the init in Docs
red/blue/green values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
Another note also there is a difference between
90/255 // wrong
and
90.0/255.0 // right
the latter is the correct one as the former will truncate the floating part as it's an integer division
Trying to simplify my implementation of coloring of each cell row object.
This is how I currently add a color to the object in each row:
progressViewLeft.primaryColor = Colors.Stage1
"Colors" is a struct set up like so:
struct Colors {
static let Stage1 = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
static let Stage2 = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
}
I'm looking for a way to instead of writing: "Colors.Stage1" and on the next one: "Colors.Stage2" something like this:
progress.primaryColor = String("Colors.Stage" + String(indexpath.row))
You can try
let allColors = [ UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0) ,
UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)]
Then
progress.primaryColor = allColors[indexpath.row]
How to prevent Xcode from displaying colors inline
let colors:[UIColor] = [
#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1),
#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1),
#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1),
#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1),
#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)
]
renders the color values in boxes inline.
How to prevent this from happening?
The other answers may prevent the color display, but make your code run slower, because every time the initialiser is used there is real code being run to create an Objective-C object. The #colorLiteral doesn't generate any code.
And I can't quite get why you are opposed to actually seeing the colors.
Use the UIColor(red:green:blue:alpha) initializer instead of color literals.
let color = UIColor(red: 0, green: 1, blue: 1, alpha: 0)
Use like below example:
static let Blue : UIColor = UIColor(red: 43.0/255.0, green: 81.0/255.0, blue: 162.0/255.0, alpha: 1.0)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have written this code into Swift but last line of code return this error message:
Cannot call value of non-function type '[UIColor]'
import Foundation
import UIKit
struct Colores {
let colores = [UIColor(red: 210/255.0, green: 90/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 40/255.0, green: 170/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 180/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 210/255.0, green: 190/255.0, blue: 5/255.0, alpha: 1),
UIColor(red: 120/255.0, green: 120/255.0, blue: 50/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 80/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 130/255.0, blue: 130/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 50/255.0, blue: 90/255.0, alpha: 1)]
func regresaColorAleatorio() -> UIColor{
let posicion = Int(arc4random()) % colores.count
return colores(posicion)
}
}
You make some syntax mistake. colores(posicion) should be colores[posicion]
struct Colores {
let colores = [UIColor(red: 210/255.0, green: 90/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 40/255.0, green: 170/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 180/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 210/255.0, green: 190/255.0, blue: 5/255.0, alpha: 1),
UIColor(red: 120/255.0, green: 120/255.0, blue: 50/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 80/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 130/255.0, blue: 130/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 50/255.0, blue: 90/255.0, alpha: 1)]
func regresaColorAleatorio() -> UIColor{
let posicion = Int(arc4random()) % colores.count
return colores[posicion]
}
}