Skip to content

Instantly share code, notes, and snippets.

@mumer92
Forked from christianselig/Locale+SFSymbol.swift
Created September 9, 2021 10:19
Show Gist options
  • Save mumer92/618bc4e4316c9e2f7de6eb0e048db3a5 to your computer and use it in GitHub Desktop.
Save mumer92/618bc4e4316c9e2f7de6eb0e048db3a5 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