Last active
September 14, 2018 06:22
-
-
Save startupcode/b4f3060aaea0cc2d5d2f4277d2798a03 to your computer and use it in GitHub Desktop.
Swift "Checkbox" class extending UIButton.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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