Skip to content

Instantly share code, notes, and snippets.

@startupcode
Last active September 14, 2018 06:22
Show Gist options
  • Save startupcode/b4f3060aaea0cc2d5d2f4277d2798a03 to your computer and use it in GitHub Desktop.
Save startupcode/b4f3060aaea0cc2d5d2f4277d2798a03 to your computer and use it in GitHub Desktop.
Swift "Checkbox" class extending UIButton.
//Here whenever isChecked value is assigned, it will update the text as per true or false condition
class CheckBox: UIButton {
var mySelectedAttributedTitle = NSAttributedString(string:" TRUE", attributes: [NSFontAttributeName: UIFont.ioniconOfSize(26), NSForegroundColorAttributeName : UIColor.blackColor()])
// Bool property
var isCheck: Bool = false {
didSet{
if isCheck == true {
//Assign true case image/text to the button like as follows
mySelectedAttributedTitle = NSAttributedString(string: "TRUE", attributes: [NSFontAttributeName: UIFont.ioniconOfSize(26), NSForegroundColorAttributeName : UIColor.blackColor()])
self.setAttributedTitle(mySelectedAttributedTitle, forState: UIControlState.Normal)
}
} else {
//Assign false case image/text to the button like as follows
mySelectedAttributedTitle = NSAttributedString(string: "FALSE", attributes: [NSFontAttributeName: UIFont.ioniconOfSize(26), NSForegroundColorAttributeName : UIColor.blackColor()])
self.setAttributedTitle(mySelectedAttributedTitle, forState: UIControlState.Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "tapCheckbox:", forControlEvents: UIControlEvents.TouchUpInside)
self.isCheck = false
}
func tapCheckbox(sender: UIButton) {
if sender == self {
isCheck = !isCheck
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment