TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift UIColor iOS Example: backgroundColor, textColor

Use the UIColor class to set the backgroundColor and textColor in an iOS program on iPhone.
UIColor. We live in a world of colors. In Swift the UIColor class can be used to apply coloring to the text and backgrounds of controls (like Labels and Buttons).
Set color. First, please create a Single View Application in Xcode. Drag a Label from the object library to the Main.storyboard. Click on the Label.

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.

Colors, options. Xcode provides five different options for setting the colors. You can even change opacity (translucency) of the control.

However: For dynamic color changes, we must use the UIColor class in Swift. With UIColor we change colors in code.

Run simulator. Please press the right arrow in Xcode to test the new colored Label. We changed the Label to have a blue text color.
Add outlet. Follow the steps to create an outlet for a Label. Please control drag a Label to the ViewController to do this. Name it "simpleLabel."Label Tutorial
Create UIColor. Here we create dynamic instances of UIColor and use them with our trusty Label. We add some code to viewDidLoad in ViewController.

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. } }
BackgroundColor, textColor. At the end of the viewDidLoad method we assign the backgroundColor and textColor properties of the Label.

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.

With UIColor, we create colors programmatically and can change an iPhone's coloration as it executes. This is powerful. We only scratched the surface of UIColor.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf