Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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