Skip to content

TCheckBox is a lightweight and elegant Swift library that enables you to create beautiful checkboxes with content labels in the fastest way possible.

License

Notifications You must be signed in to change notification settings

fanta1ty/TCheckBox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LOGO

TCheckBox

Swift 5.0 Version License Platform Email

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.

✨ Features

  • πŸš€ 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

🎯 Why TCheckBox?

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.

πŸ“‹ Requirements

  • iOS: 10.0+
  • Swift: 5.0+
  • Xcode: 10.0+

πŸ“¦ Installation

CocoaPods

CocoaPods is the recommended way to install TCheckBox. Add the following line to your Podfile:

pod 'TCheckBox'

Then run:

pod install

Manual Installation

  1. Download the project files
  2. Drag and drop the TCheckBox source files into your Xcode project
  3. Make sure to add them to your target

πŸš€ Quick Start

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)
        ])
    }
}

πŸ“– Usage Guide

Basic Setup

Creating a checkbox is as simple as:

import TCheckBox

let checkBox = TCheckBox()
checkBox.contentLabel.text = "Your checkbox text here"

Setting Content

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

Managing Checkbox State

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()

Reading Checkbox State

Check if the checkbox is currently selected:

if checkBox.checkButton.isSelected {
    print("Checkbox is checked")
} else {
    print("Checkbox is unchecked")
}

🎨 Advanced Usage

Custom Styling

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

Multiple Checkboxes

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...
    }
}

Handling User Interaction

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()
    }
}

Form Validation

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()
        }
    }
}

πŸ’‘ Best Practices

  1. Always set meaningful text for the content label to improve accessibility
  2. Use appropriate font sizes for better readability
  3. Consider multi-line support for longer text content
  4. Implement proper validation when using checkboxes in forms
  5. Provide visual feedback when checkbox state changes
  6. Group related checkboxes using stack views or container views

🎯 Use Cases

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

πŸ”§ Example Project

To run the example project:

  1. Clone the repository:
    git clone https://github.com/fanta1ty/TCheckBox.git
  2. Navigate to the Example directory:
    cd TCheckBox/Example
  3. Install dependencies:
    pod install
  4. Open the workspace file:
    open TCheckBox.xcworkspace
  5. Build and run the project

🀝 Contributing

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.

Development Guidelines

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ” Comparison with Alternatives

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

πŸ“ž Support

If you encounter any issues or have questions:

  1. Check the Issues page
  2. Create a new issue with detailed information
  3. Contact the maintainer at [email protected]

πŸ“š Documentation

For detailed documentation and advanced usage examples, visit the Wiki.

πŸ† Acknowledgments

  • The iOS developer community for inspiration and feedback
  • Contributors who help improve the library
  • Developers who test and provide valuable feedback

πŸ“„ License

TCheckBox is available under the MIT license. See the LICENSE file for more information.

πŸ‘€ Author

fanta1ty


Made with ❀️ for the iOS development community

If TCheckBox helps you build better apps, please consider giving it a ⭐️ on GitHub!

til

About

TCheckBox is a lightweight and elegant Swift library that enables you to create beautiful checkboxes with content labels in the fastest way possible.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •