lukepistrol/SFSymbolsMacro
소개
A Swift Macro for "type-safe" SF Symbols.
웹 및 앱 개발측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalSFSymbolsMacro
This Swift Macro provides an easy way to make the use of SF Symbols in Swift more or less "type-safe".
Installation
Xcode
- Click File > Add Package Dependencies
- Paste the following link into the search field on the upper-right:
https://github.com/lukepistrol/SFSymbolsMacro.git
Swift Package Manager
In Package.swift:
dependencies: [
.package(url: "https://github.com/lukepistrol/SFSymbolsMacro.git", from: "0.1.0")
]
And then add the dependency to your targets.
Usage
Simply create an enum which will hold all the SF Symbols for your project:
enum Symbols: String {
case circle
case circleFill = "circle.fill"
case shareIcon = "square.and.arrow.up"
case globe
}
Then simply import SFSymbolsMacro and add the @SFSymbol macro annotation to the enum:
import SFSymbolsMacro
import SwiftUI
@SFSymbol
enum Symbols: String { ... }
The macro will then validate each case and the expanded macro will look something like this:
enum Symbols: String {
case circle
case circleFill = "circle.fill"
case shareIcon = "square.and.arrow.up"
case globe
var image: Image {
Image(systemName: self.rawValue)
}
var name: String {
self.rawValue
}
#if canImport(UIKit)
func uiImage(configuration: UIImage.Configuration? = nil) -> UIImage {
UIImage(systemName: self.rawValue, withConfiguration: configuration)!
}
#else
func nsImage(accessibilityDescription: String? = nil) -> NSImage {
return NSImage(systemSymbolName: self.rawValue, accessibilityDescription: accessibilityDescription)!
}
#endif
func callAsFunction() -> String {
return self.rawValue
}
}
In your code you can then call a symbol:
var body: some View {
VStack {
Symbols.circleFill.image
Label("Globe", systemImage: Symbols.globe.name)
// the above can also be written as
Label("Globe", systemImage: Symbols.globe())
}
}
In case the provided raw value is not a valid SF Symbol, Xcode will show a compile error at the enum-case in question:
Contribution
If you have any ideas on how to take this further I'm happy to discuss things in an issue.
관련 프로젝트 살펴보기
불러오는 중…
