Skip to content

Instantly share code, notes, and snippets.

@marcoarment
Last active October 26, 2023 17:13
Show Gist options
  • Save marcoarment/fdd9c19995e8f9d16bc2d10cd8cefe39 to your computer and use it in GitHub Desktop.
Save marcoarment/fdd9c19995e8f9d16bc2d10cd8cefe39 to your computer and use it in GitHub Desktop.
SwiftUI Text with inline SF Symbols
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