2021 SwiftUI & UI Frameworks
WWDC21 · 15 min · SwiftUI & UI Frameworks
SF Symbols in UIKit and AppKit
Learn how you can create colorized symbols with SF Symbols 3 and customize them to match the visual design of your app’s interface. We’ll take you through the latest UIKit and AppKit APIs for integrating colorized symbols, as well as best practices for implementation. To get the most out of this session, we recommend watching “Introducing SF Symbols” from WWDC19.
Watch at developer.apple.com ↗Code shown on screen · 18 snippets
Monochrome symbols
// Play
let playImage = UIImage(systemName: "play")
playImageView.image = playImage
playImageView.tintColor = .systemBlue Hierarchical symbols
// Device image
var image = NSImage(systemSymbolName: "ipad.landscape",
accessibilityDescription: "iPad")
let config = NSImage.SymbolConfiguration(hierarchicalColor: .label)
deviceView.image = image
deviceView.symbolConfiguration = config Setup button configurations
// Initialize button configuration
let speakerConfig = UIButtonConfiguration.plain
speakerConfig.image = UIImage(systemName: "speaker.wave.2")
let callConfig = UIButtonConfiguration.plain
callConfig.image = UIImage(systemName: "phone")
let deleteConfig = UIButtonConfiguration.plain
deleteConfig.image = UIImage(systemName: "trash") Image variants
// Button container view
actionsView.imageVariant = .none Image variants
// Button container view
actionsView.imageVariant = .circle Image variants
// Button container view
actionsView.imageVariant = .circle.fill Speaker button color configuration
// Speaker button color configuration
let config = UIImage.SymbolConfiguration(paletteColors: [.tintColor, .systemGray2])
speakerConfig.preferredSymbolConfigurationForImage = config
speakerButton.configuration = speakerConfig Call button color configuration
// Call button color configuration
let config = UIImage.SymbolConfiguration(paletteColors: [.white, .tintColor])
callConfig.preferredSymbolConfigurationForImage = config
callButton.configuration = callConfig Delete button color configuration
// Delete button color configuration
let config = UIImage.SymbolConfiguration(paletteColors: [.white, .systemRed])
deleteConfig.preferredSymbolConfigurationForImage = config
deleteButton.configuration = deleteConfig Colors matter
// Colors matter!
let config = UIImage.SymbolConfiguration(paletteColors: [.tintColor, .systemGray2])
let config = UIImage.SymbolConfiguration(paletteColors: [.white, .tintColor])
let config = UIImage.SymbolConfiguration(paletteColors: [.white, .systemRed]) Tint color
view.backgroundColor = .tintColor
label.textColor = .tintColor
searchField.tokenBackgroundColor = .tintColor
tabBarItem.badgeColor = .tintColor Multicolor symbols
// configure table view cell
let image = UIImage(systemName: category.iconName)
cell.imageView.image = image Multicolor symbols
// configure table view cell
let image = UIImage(systemName: category.iconName)
let config = UIImage.SymbolConfiguration.preferringMultiColor
let tintColor = category.colorForIcon
cell.imageView.image = image
cell.imageView.preferredSymbolConfiguration = config
cell.imageView.tintColor = tintColor Multicolor symbols
// configure table view cell
let image = UIImage(systemName: category.iconName)
let config = UIImage.SymbolConfiguration.preferringMultiColor
let tintColor = category.colorForIcon
cell.imageView.image = image
cell.imageView.preferredSymbolConfiguration = config
cell.imageView.tintColor = tintColor Combining configurations
// combined configuration
let image = UIImage(systemImage: "ipad.and.iphone")
headerView.image = image Combining configurations
// Combined configuration
let image = UIImage(systemImage: "ipad.and.iphone")
headerView.image = image
let fontConfig = UIImage.SymbolConfiguration(pointSize: 60, scale: .large)
let colorConfig = UIImage.SymbolConfiguration(hierarchicalColor: .systemBlue)
let config = fontConfig.applying(colorConfig)
headerView.preferredSymbolConfiguration = config Symbols in attributed strings
// Hotel amenities
let amenitiesString = NSMutableAttributedString(...)
if (room.amenities.contains(.tv)) {
let config = UIImage.SymbolConfiguration(
hierarchicalColor: .systemGreen)
let tvImage = UIImage(systemImage: "tv",
withConfiguration: config)
let attachment = NSTextAttachment(image: tvImage)
let attachmentString = NSAttributedString(attachment:
attachment)
let tvString = attachmentString.mutableCopy()
tvString.append(NSAttributedString(" TV, ")
amenitiesString.append(tvString)
} Symbols in attributed strings
// hotel amenities
let amenitiesLabel = UILabel()
amenitiesLabel.textColor = .systemGreen
amenitiesLabel.font = UIFont.systemFont(ofSize: 25)
amenitiesLabel.attributedString = amenitiesString Resources
Related sessions
-
27 min -
26 min -
13 min -
11 min