IBInspectable setTitle: forState on UIButton doesn't work
I am trying to implement a localizable class for UIButtons using live rendering in Interface Builder. This is the code I have so far:
@IBDesignable class TIFLocalizableButton: UIButton {
@IBInspectable var localizeString:String = "" {
didSet {
#if TARGET_INTERFACE_BUILDER
var bundle = NSBundle(forClass: self.dynamicType)
self.setTitle(bundle.localizedStringForKey(self.localizeString, value:"", table: nil), forState: UIControlState.Normal)
#else
self.setTitle(self.localizeString.localized(), forState: UIControlState.Normal)
#endif
}
}
}
The layout updates correctly in IB, but no text is displayed. I created the same implementation with UILabel that works: https://github.com/AvdLee/ALLocalizableLabel
Any ideas on how to fix this are greatly appreciated!
+3
source to share
3 answers
At WWDC, I was able to ask one of the engineers. It turns out to be corrected in this way:
override func setTitle(title: String?, forState state: UIControlState) {
#if TARGET_INTERFACE_BUILDER
let bundle = NSBundle(forClass: self.dynamicType)
super.setTitle(bundle.localizedStringForKey(self.localizeString, value:"", table: nil), forState: UIControlState.Normal)
#else
super.setTitle(title, forState: state)
#endif
}
Because the frontend developer calls setTitle multiple times for different states.
0
source to share