Skip to content

Instantly share code, notes, and snippets.

@damijanracel
Created October 9, 2018 11:27
Show Gist options
  • Save damijanracel/d4951ffada17ff575cd75ed9a7b6be47 to your computer and use it in GitHub Desktop.
Save damijanracel/d4951ffada17ff575cd75ed9a7b6be47 to your computer and use it in GitHub Desktop.
UILabel extension for localization
extension UILabel {
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
private struct AssociatedKeys {
static var localizationKey = "nsh_localizationKey"
}
var localizationKey: LocalizationKey? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.localizationKey) as? LocalizationKey
}
set {
if let newValue = newValue {
objc_setAssociatedObject(self, &AssociatedKeys.localizationKey, newValue as LocalizationKey?,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment