Last active
October 26, 2023 17:13
-
-
Save marcoarment/fdd9c19995e8f9d16bc2d10cd8cefe39 to your computer and use it in GitHub Desktop.
SwiftUI Text with inline SF Symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension Text { | |
public struct InlineSymbol { | |
public let name: String | |
public let accessibilityLabel: String | |
public let color: Color? | |
public init(name: String, accessibilityLabel: String, color: Color? = nil) { | |
self.name = name | |
self.accessibilityLabel = accessibilityLabel | |
self.color = color | |
} | |
} | |
public static func withSymbolPrefixes(symbols: [InlineSymbol], text: String) -> Text { | |
var strText = Text(text) | |
for symbol in symbols.reversed() { | |
var symbolText = | |
Text(Image(systemName: symbol.name)) | |
.accessibilityLabel(symbol.accessibilityLabel + ", ") | |
if let color = symbol.color { | |
symbolText = symbolText.foregroundColor(color) | |
} | |
strText = symbolText + Text(" ") + strText | |
} | |
return strText | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment