Colors in Swift: UIColor

UIColor makes it possible to customize the color for Dark Mode, gives a semantic set of colors "from Apple", and also makes it possible to set not only the color, but also the pattern of filling the space, which kind of shouts to us that UIColor is not a color at all. Let's take a quick look at these UIColor halls.





UIColor is a class that stores color and transparency data. Color in iOS can be represented in different color spaces, the most famous of which is RGB. In fact, the color itself is stored in the cgColor property of the CGColor class. There are several entities in iOS that can store color information - each for its own framework:





  • for UIKit it is the UIColor class, 





  • for SwiftUI it is a Color structure, 





  • for Core Graphics this is the CGColor class, 





  • for Core Image this is the CIColor class





In this article, I will try to talk about the UIColor class, which is part of the UIKit framework.





System colors

UIKit has some predefined colors that you can use in your project. For example, green UIColor.green or often just .green is enough. Writing like this will use a specific color with RGB values โ€‹โ€‹of 0.0, 1.0, 0.0 and a transparency level of 1.0. But in UIColor, the color UIColor.systemGreen is also defined. How is it different from the usual UIColor.green? 





Since iOS 13.0, Dark Mode has appeared. The user can choose in the system settings in which color theme it is more comfortable for him to work - in a light theme or in a dark one. And the green color for these modes will be slightly different. Moreover, for a comfortable perception, the user can choose a high-contrast mode that increases the contrast between the interface elements and the background. And the green color for this mode will also be different. 





, , , Apple, , . - , , "system...":





  • systemBlue





  • systemIndigo





  • systemOrange





  • systemPink





  • systemPurple





  • systemRed





  • systemTeal





  • systemYellow





- - , , .





, , - , , ..





  • systemGray





  • systemGray2





  • systemGray3





  • systemGray4





  • systemGray5





  • systemGray6





, , . .





Black White, . .





, . UIKit tintColor, , - , , , UISegmentControl .. - , brandColor primaryBrandColor/secondaryBrandColor. .





, - . , UILabel . , UILabel .red, :





let primaryTextColor = UIColor.red







UILabel :





label.textColor = primaryTextColor







- , . primaryTextColor.





. , .





UIKit

Apple , . , . UIKit .





:





  • label -





  • secondaryLabel -





  • tertiaryLabel -





  • quaternaryLabel -





, , secondaryLabel , tertiaryLabel . 





:





  • systemFill





  • secondarySystemFill





  • tertiarySystemFill





  • quaternarySystemFill





Apple / , , ..





:





  • placeholderText





:





  • systemBackground





  • secondarySystemBackground





  • tertiarySystemBackground





.





:





  • systemGroupedBackground





  • secondarySystemGroupedBackground





  • tertiarySystemGroupedBackground





.grouped .





:





  • separator





  • opaqueSeparator





:





  • link





, :





  • darkText





  • lightText





UIKit , , . 





Apple.





, Dark Light

- XCode Assets Color Set, Dark Light Mode Appearances. , .





Where can I find color settings in Assets in Xcode
Assets XCode

:





let color = UIColor(named: "ColorName")







" " iOS.





- , .





UIColor:





init(dynamicProvider: @escaping (UITraitCollection) -> UIColor)







UITraitCollection - , Dark Light , .. userInterfaceStyle .





:





let color = UIColor { traitCollection -> UIColor in







    switch traitCollection.userInterfaceStyle {







        case .light, .unspecified: return .white







        case .dark: return .black







    }







}







UIColor :





  • HSB, RGB





  • UIColor









  • - UIColor ...





init(patternImage image: UIImage)







UIColor, , . , , UILabel, -  (. ).





- , . " ", " ".





Although UIColor is a fairly simple class, it provides color management capabilities based on the current settings. In addition, as we saw, it can set not only the color, but also the overall method of filling the space. To help developers, Apple provides a set of semantic colors that simplify development and allow you to slightly change the color of interface elements.








All Articles