*Swift5, Xcode ver 12.1
The code changes the Text color in System color, RGB, and color code.
Please refer to it when changing the text color of labels and text.
Specify a color using System color
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// white
label.textColor = UIColor.white
// black
label.textColor = UIColor.black
// red
label.textColor = UIColor.red
//Available on iOS 13 and above
label.textColor = UIColor.systemGray2
label.textColor = UIColor.secondaryLabel
}
Select Text color from Storyboard to see a list of other colors
Specify the color with RGB (0 ~ 1.0)
Specify the RGB value of Swift’s UI color from 0 to 1.0 instead of 255 steps.
Write the following code in viewDidLoad
// rgba = UIColor (red, green, blue, transparency)
// Set alpha to 0.0 to make Text transparent
let rgba = UIColor(red: 0.1, green: 0.7, blue: 0.9, alpha: 1.0)
label.textColor = rgba
Specify the color in RGB (0 to 255)
Even though I know the RGB value from 0 to 255, I don’t want to use it too much because it is troublesome to calculate and adjust each time if it is RGB (0 to 1.0) mentioned above. .. ..
In such a case, it is convenient to use the RGB value as it is by using a code that divides the known RGB value by 255. Write the following code in viewDidLoad
// rgba = UIColor (red, green, blue, transparency)
// Set alpha to 0.0 to make Text transparent
let rgba = UIColor(red: 129/255, green: 216/255, blue: 208/255, alpha: 1.0)
explain.textColor = rgba
Specify the color code
However, it is easier to specify by color code, so prepare the following extensions.
It is recommended to make an extension once because it is quite convenient because it can be used in various situations.
Create a new SwiftFile
File → New → File … → Select Swift File and enter an appropriate name such as UIColorHex in Save as → Decide the save destination and Create
The following code in the created file Paste.
import UIKit
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1.0) {
let v = Int("000000" + hex, radix: 16) ?? 0
let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
self.init(red: r, green: g, blue: b, alpha: min(max(alpha, 0), 1))
}
}
Describe the following in viewDidLoad of the class you want to use
Call UIColor and put the color code without # in hex to use
label.textColor = UIColor(hex: "333333")
label.textColor = UIColor(hex: "333333", alpha: 1.0)
Click here to specify the color code in the custom class
import UIKit
class CustomLabelText: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
customText()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
customText()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
customText()
}
private func customText() {
textColor = UIColor(hex: "333333", alpha: 1.0)
font = UIFont(name: "Helvetica Neue", size: 17)
}
}
If you find it helpful, please like one button! !! 😉 Thank you!!!
コメント