Skip to content

Commit c41d0df

Browse files
Merge pull request #2 from ActuallyTaylor/ActuallyTaylor-patch-1
Update the Package Scope
2 parents c583b2a + 50cdf5c commit c41d0df

19 files changed

Lines changed: 271 additions & 5 deletions

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.8
1+
// swift-tools-version: 5.7.1
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

README.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,128 @@
11
# SFSymbols
2-
A swift enumeration that can be used to access SF Symbols without having to use their string names.
2+
SFSymbols gives you access to Apple's SFSymbol library in a type safe and Swifty manner. It get's difficult to remember all of the correct symbol names when using strings to label everything. This enumeration makes it easy to use a type safe interface with SFSymbols with SwiftUI, UIKit and AppKit.
33

44
## Install
55
Add the following URL to Swift Package Manager:
66
```
77
https://github.com/ActuallyTaylor/SFSymbols
88
```
99

10-
## How to use
10+
## Basic Usage
1111
```swift
12+
import SwiftUI
1213
import SFSymbols // Import SFSymbols
1314

14-
Image(.hammer) // Boom 💥, it is that simple.
15+
Image(.sparkles) // Boom 💥, it is that simple.
1516
```
1617

17-
### Custom Images
18+
#### Custom Images
1819
```swift
20+
import SwiftUI
1921
import SFSymbols
2022

2123
Image(.custom("spaceship")) // Will use a custom symbol file located in your assets folder!
2224
```
25+
26+
## Advanced Usage
27+
### SwiftUI
28+
#### Images
29+
The default Image initializer is replaced with a custom symbol initializer.
30+
31+
```swift
32+
Image(.hammer)
33+
```
34+
35+
#### Buttons
36+
Two new initializers have been added to Buttons that allow you to initialize them automatically with an SFSymbol label, reducing the amount of code in your views.
37+
```swift
38+
// Basic Button initializer
39+
Button(.fireplace_fill) {
40+
print("Sit by the fireplace with me!")
41+
}
42+
43+
// Button initializer with a role
44+
Button(.xmark_app_fill, role: .cancel) {
45+
print("Goodbye!")
46+
}
47+
```
48+
49+
#### Menus
50+
Two new initializers have been added to Menus that allow you to automatically create them with an SFSymbol image label.
51+
```swift
52+
// Basic menu initializer
53+
Menu(.command) {
54+
Button("Hello World!") {
55+
print("Hello World!")
56+
}
57+
}
58+
59+
// Menu initializer with a primary action
60+
Menu(.command) {
61+
Button("Hello World!") {
62+
print("Hello World!")
63+
}
64+
} primaryAction: {
65+
print("Hello World!")
66+
}
67+
```
68+
69+
#### Labels
70+
Labels have been given two initializers that allow you to create them with an SFSymbol.
71+
72+
```swift
73+
// Just a string
74+
Label("Hello World", symbol: .figure_wave)
75+
76+
// Localized string key
77+
Label(LocalizedStringKey("hello.world"), symbol: .figure_wave)
78+
```
79+
80+
### UIKit
81+
#### UIImage
82+
UIImages have been given a set of initializers that allow you too use SFSymbols directly
83+
84+
```swift
85+
UIImage(.sparkles)
86+
UIImage(.sparkles, withConfiguration: /* some UIImage Configuration */)
87+
UIImage(.sparkles, compatibleWith: .current)
88+
```
89+
90+
#### UIButton
91+
UIButtons have been given a system button initializer along with a way to set the image directly to an SFSymbol instead of needing to create a UIImage.
92+
93+
```swift
94+
// Create a System Button with an SFSymbol
95+
UIButton.systemButton(with: .figure_wave, target: self, action: /* any selector */)
96+
97+
// Set an image for a certain state
98+
UIButton.setImage(.figure_wave, for: .normal)
99+
```
100+
101+
#### DisplayRepresentation.Image
102+
DisplayRepresentation images have been given initializers. These are only available when you are importing both UIKit and AppIntents.
103+
104+
```swift
105+
DisplayRepresentation.Image(.sparkles, isTemplate: false)
106+
DisplayRepresentation.Image(.sparkles, tintColor: .red, symbolConfiguration: /* some symbol configuration */)
107+
```
108+
109+
#### UIApplicationShortcutIcon
110+
UIApplicationShortcutIcon have been given an initializer so they can be created with an SFSymbol.
111+
112+
```swift
113+
UIApplicationShortcutIcon(.sparkles)
114+
```
115+
116+
### AppKit
117+
#### NSImage
118+
NSImages have been given an initializer allowing you to create them using an SFSymbol.
119+
120+
```swift
121+
NSImage(.sparkles, variableValue: 1.0)
122+
```
123+
124+
#### NSButton
125+
NSButtons have been given an initializer that allows you to initialize them with an SFSymbol. This initializer allows you to setup the button to automatically use an SFSymbol with the given variable value.
126+
```swift
127+
NSButton(.sparkles, variableValue: 1.0, target: self, action: /* some selector */)
128+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if canImport(AppKit)
2+
import AppKit
3+
4+
public extension NSButton {
5+
@available(macOS 13.0, *)
6+
convenience init(_ symbol: SFSymbol, variableValue: Double, target: Any?, action: Selector?) {
7+
let image = NSImage(symbol, variableValue: variableValue)
8+
9+
self.init(image: image, target: target, action: action)
10+
}
11+
}
12+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if canImport(AppKit)
2+
import AppKit
3+
4+
@available(macOS 13.0, *)
5+
public extension NSImage {
6+
convenience init(_ symbol: SFSymbol, variableValue: Double) {
7+
switch symbol {
8+
case .custom(let name):
9+
self.init(named: name)!
10+
default:
11+
self.init(symbolName: symbol.name, variableValue: variableValue)!
12+
}
13+
14+
}
15+
}
16+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SwiftUI
2+
3+
public extension Button {
4+
init(_ symbol: SFSymbol, action: @escaping () -> Void) where Label == Image {
5+
self = Button(action: action, label: {
6+
Image(symbol)
7+
})
8+
}
9+
10+
@available(iOS 15.0, macOS 12.0, *)
11+
init(_ symbol: SFSymbol, role: ButtonRole?, action: @escaping () -> Void) where Label == Image {
12+
self = Button(role: role, action: action, label: {
13+
Image(symbol)
14+
})
15+
}
16+
}
File renamed without changes.
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import SwiftUI
2+
3+
@available(iOS 14.0, *)
4+
public extension Menu {
5+
init(_ symbol: SFSymbol, content: @escaping () -> Content) where Label == Image, Content : View {
6+
self = Menu(content: content, label: {
7+
Image(symbol)
8+
})
9+
}
10+
11+
@available(iOS 15.0, macOS 12.0, *)
12+
init(_ symbol: SFSymbol, content: @escaping () -> Content, primaryAction: @escaping () -> Void) where Label == Image, Content : View {
13+
self = Menu(content: content, label: {
14+
Image(symbol)
15+
}, primaryAction: primaryAction)
16+
}
17+
18+
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#if canImport(UIKit) && canImport(AppIntents)
2+
import AppIntents
3+
import UIKit
4+
5+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
6+
extension DisplayRepresentation.Image {
7+
init(_ symbol: SFSymbol, tintColor: UIColor? = nil, symbolConfiguration: UIImage.SymbolConfiguration? = nil) {
8+
switch symbol {
9+
case .custom(let name):
10+
self.init(named: name)
11+
default:
12+
self.init(systemName: symbol.name, tintColor: tintColor, symbolConfiguration: symbolConfiguration)!
13+
}
14+
}
15+
16+
init(_ symbol: SFSymbol, isTemplate: Bool?) {
17+
switch symbol {
18+
case .custom(let name):
19+
self.init(named: name)
20+
default:
21+
self.init(systemName: symbol.name, isTemplate: isTemplate)
22+
}
23+
}
24+
}
25+
26+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#if canImport(UIKit)
2+
import UIKit
3+
4+
extension UIApplicationShortcutIcon {
5+
convenience init(_ symbol: SFSymbol) {
6+
switch symbol {
7+
case .custom(let name):
8+
self.init(templateImageName: name)
9+
default:
10+
self.init(systemImageName: symbol.name)
11+
}
12+
}
13+
}
14+
#endif

0 commit comments

Comments
 (0)