TCheckBox is a lightweight and elegant Swift library that enables you to create beautiful checkboxes with content labels in the fastest way possible. Designed for simplicity and efficiency, TCheckBox eliminates the need for writing complex code while providing a clean and intuitive checkbox component for your iOS applications.
- π Simple Integration: Create checkboxes with just a few lines of code
- π Built-in Content Label: Integrated text label alongside checkbox
- π¨ Clean Design: Modern and sleek checkbox appearance
- β‘ Lightweight: Minimal footprint with maximum functionality
- π§ Easy Customization: Simple API for state management
- π± iOS Optimized: Native UIKit integration
- π― Production Ready: Thoroughly tested and reliable
Traditional checkbox implementation in iOS requires:
- Multiple UI components setup
- Complex constraint management
- State synchronization between checkbox and label
- Boilerplate code repetition
TCheckBox solves these problems by providing a single component that handles everything for you.
- iOS: 10.0+
- Swift: 5.0+
- Xcode: 10.0+
CocoaPods is the recommended way to install TCheckBox. Add the following line to your Podfile
:
pod 'TCheckBox'
Then run:
pod install
- Download the project files
- Drag and drop the
TCheckBox
source files into your Xcode project - Make sure to add them to your target
import TCheckBox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupCheckBox()
}
private func setupCheckBox() {
// Create TCheckBox instance
let checkBox = TCheckBox()
// Set content text
checkBox.contentLabel.text = "I agree to the terms and conditions"
// Add to view
view.addSubview(checkBox)
// Setup constraints (example)
checkBox.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
checkBox.centerXAnchor.constraint(equalTo: view.centerXAnchor),
checkBox.centerYAnchor.constraint(equalTo: view.centerYAnchor),
checkBox.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20),
checkBox.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -20)
])
}
}
Creating a checkbox is as simple as:
import TCheckBox
let checkBox = TCheckBox()
checkBox.contentLabel.text = "Your checkbox text here"
Use the built-in contentLabel
to display text alongside your checkbox:
checkBox.contentLabel.text = "Subscribe to newsletter"
checkBox.contentLabel.font = UIFont.systemFont(ofSize: 16)
checkBox.contentLabel.textColor = .darkGray
Control the checked/unchecked state using the isSelected
property:
// Check the checkbox
checkBox.checkButton.isSelected = true
// Uncheck the checkbox
checkBox.checkButton.isSelected = false
// Toggle state
checkBox.checkButton.isSelected.toggle()
Check if the checkbox is currently selected:
if checkBox.checkButton.isSelected {
print("Checkbox is checked")
} else {
print("Checkbox is unchecked")
}
Customize the appearance of your checkbox:
let checkBox = TCheckBox()
// Customize the label
checkBox.contentLabel.text = "Enable notifications"
checkBox.contentLabel.font = UIFont.boldSystemFont(ofSize: 16)
checkBox.contentLabel.textColor = .black
checkBox.contentLabel.numberOfLines = 0 // Multi-line support
// Access the checkbox button for further customization
checkBox.checkButton.tintColor = .systemBlue
Create multiple checkboxes for forms or settings:
class SettingsViewController: UIViewController {
private let options = [
"Enable push notifications",
"Allow location access",
"Share usage analytics",
"Auto-save documents"
]
private var checkBoxes: [TCheckBox] = []
override func viewDidLoad() {
super.viewDidLoad()
setupCheckBoxes()
}
private func setupCheckBoxes() {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 16
stackView.alignment = .leading
for option in options {
let checkBox = TCheckBox()
checkBox.contentLabel.text = option
checkBoxes.append(checkBox)
stackView.addArrangedSubview(checkBox)
}
view.addSubview(stackView)
// Add constraints for stackView...
}
}
Add target-action for checkbox interaction:
checkBox.checkButton.addTarget(self, action: #selector(checkBoxTapped(_:)), for: .touchUpInside)
@objc private func checkBoxTapped(_ sender: UIButton) {
print("Checkbox tapped, new state: \(sender.isSelected)")
// Update UI or perform actions based on state
if sender.isSelected {
// Handle checked state
showConfirmationMessage()
} else {
// Handle unchecked state
hideConfirmationMessage()
}
}
Use TCheckBox in forms with validation:
class FormViewController: UIViewController {
@IBOutlet weak var termsCheckBox: TCheckBox!
@IBOutlet weak var newsletterCheckBox: TCheckBox!
@IBOutlet weak var submitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
setupForm()
}
private func setupForm() {
termsCheckBox.contentLabel.text = "I accept the Terms of Service"
newsletterCheckBox.contentLabel.text = "Subscribe to our newsletter"
// Add validation targets
termsCheckBox.checkButton.addTarget(self, action: #selector(validateForm), for: .touchUpInside)
newsletterCheckBox.checkButton.addTarget(self, action: #selector(validateForm), for: .touchUpInside)
validateForm()
}
@objc private func validateForm() {
// Enable submit button only if terms are accepted
submitButton.isEnabled = termsCheckBox.checkButton.isSelected
submitButton.alpha = submitButton.isEnabled ? 1.0 : 0.5
}
@IBAction func submitButtonTapped(_ sender: UIButton) {
if termsCheckBox.checkButton.isSelected {
// Process form submission
processForm()
}
}
}
- Always set meaningful text for the content label to improve accessibility
- Use appropriate font sizes for better readability
- Consider multi-line support for longer text content
- Implement proper validation when using checkboxes in forms
- Provide visual feedback when checkbox state changes
- Group related checkboxes using stack views or container views
TCheckBox is perfect for:
- Terms and Conditions: Agreement checkboxes in registration forms
- Settings Screens: Enable/disable feature toggles
- Survey Forms: Multiple choice questions
- Shopping Lists: Item completion tracking
- Task Management: Todo item completion
- User Preferences: Notification and privacy settings
- Onboarding Flows: Feature opt-in selections
To run the example project:
- Clone the repository:
git clone https://github.com/fanta1ty/TCheckBox.git
- Navigate to the Example directory:
cd TCheckBox/Example
- Install dependencies:
pod install
- Open the workspace file:
open TCheckBox.xcworkspace
- Build and run the project
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Feature | TCheckBox | UISwitch | Custom Implementation |
---|---|---|---|
Simplicity | β One-liner setup | β Requires separate label | β Complex setup |
Built-in Label | β Integrated | β Manual setup needed | β Manual implementation |
Checkbox Style | β Native checkbox look | β Switch appearance | β Fully customizable |
Code Reduction | β Minimal code | β More boilerplate | β Significant code |
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Contact the maintainer at [email protected]
For detailed documentation and advanced usage examples, visit the Wiki.
- The iOS developer community for inspiration and feedback
- Contributors who help improve the library
- Developers who test and provide valuable feedback
TCheckBox is available under the MIT license. See the LICENSE file for more information.
fanta1ty
- GitHub: @fanta1ty
- Email: [email protected]
Made with β€οΈ for the iOS development community
If TCheckBox helps you build better apps, please consider giving it a βοΈ on GitHub!