C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: We can set the color of the text directly in Xcode. This is not dynamic, but it can be used to provide a default color.
Blue: Try setting the text of the Label to blue. You may want to resize, center-align and change the text of the Label.
However: For dynamic color changes, we must use the UIColor class in Swift. With UIColor we change colors in code.
UIColor: We use an init method on UIColor that receives red, green, blue and alpha arguments. These are all doubles.
Tip: The arguments are from 0 to 1. For more of a certain kind of color, use a value closer to 1. For less, use 0.
Alpha: For color2 we use an alpha of 0.5. We use 0 for the color arguments. So this is a slightly translucent black.
Swift program that uses IColor
//
// ViewController.swift
// ExampleTime
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// ... Create two UIColors.
let color = UIColor(red: 0.5,
green: 1,
blue: 1,
alpha: 1)
// This is a translucent black.
let color2 = UIColor(red: 0,
green: 0,
blue: 0,
alpha: 0.5)
// Set background.
simpleLabel.backgroundColor = color
// Set foreground.
simpleLabel.textColor = color2
}
@IBOutlet weak var simpleLabel: UILabel!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Next: Run the iOS simulator. We find the background of the button is the blue-green (aquamarine) color we specified.
And: The text color is a black that is only partially opaque. So the text darkens its background.