Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created September 3, 2021 21:54
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christianselig/09383de004bd878ba4a86663bc6d1b0b to your computer and use it in GitHub Desktop.
Save christianselig/09383de004bd878ba4a86663bc6d1b0b to your computer and use it in GitHub Desktop.
Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
extension Locale {
/// Returns an SF Symbol currency image that match's the device's current locale, for instance dollar in North America, Indian rupee in India, etc.
func currencySFSymbol(filled: Bool, withConfiguration configuration: UIImage.Configuration? = nil) -> UIImage {
// Default currency symbol will be the Animal Crossing Leaf coin 􁂬 to remain impartial to any specific country
let defaultSymbol = UIImage(systemName: "leaf.circle\(filled ? ".fill" : "")")!
guard let currencySymbolName = currencySymbolNameForSFSymbols() else { return defaultSymbol }
let systemName = "\(currencySymbolName).circle\(filled ? ".fill" : "")"
return UIImage(systemName: systemName, withConfiguration: configuration) ?? defaultSymbol
}
private func currencySymbolNameForSFSymbols() -> String? {
guard let currencySymbol = currencySymbol else { return nil }
let symbols: [String: String] = [
"$": "dollar",
"¢": "cent",
"¥": "yen",
"£": "sterling",
"₣": "franc",
"ƒ": "florin",
"₺": "turkishlira",
"₽": "ruble",
"€": "euro",
"₫": "dong",
"₹": "indianrupee",
"₸": "tenge",
"₧": "peseta",
"₱": "peso",
"₭": "kip",
"₩": "won",
"₤": "lira",
"₳": "austral",
"₴": "hryvnia",
"₦": "naira",
"₲": "guarani",
"₡": "coloncurrency",
"₵": "cedi",
"₢": "cruzeiro",
"₮": "tugrik",
"₥": "mill",
"₪": "shekel",
"₼": "manat",
"₨": "rupee",
"฿": "baht",
"₾": "lari",
"R$":" brazilianreal"
]
guard let currencySymbolName = symbols[currencySymbol] else { return nil }
return "\(currencySymbolName)sign"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment