Checkbox.swift 1.69 KB
//
//  Checkbox.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 07.06.2022.
//

import Foundation
import UIKit
class Checkbox: UIButton {
    
    private let checkboxImageView: UIImageView = {
        let obj = UIImageView()
        return obj
    }()
    
    private let checkBoxLabel: UILabel = {
        let obj = UILabel()
        obj.font = .skModernist(type: .regular, ofSize: 15)
        return obj
    }()
    
    override var isSelected: Bool {
        didSet {
            checkBoxLabel.textColor = isSelected ?
            UIColor(red: 50/255, green: 55/255, blue: 85/255, alpha: 1) :
            UIColor(red: 185/255, green: 185/255, blue: 185/255, alpha: 1)
            checkboxImageView.image = isSelected ?
            UIImage(named: "OnIndicator") :
            UIImage(named: "OffIndicator")
        }
    }
    
    var checkboxTitle: String = "" {
        didSet {
            checkBoxLabel.text = checkboxTitle
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    private func setup() {
        isSelected = false
        
        addSubview(checkboxImageView)
        addSubview(checkBoxLabel)
        
        checkboxImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        checkBoxLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(checkboxImageView.snp.trailing).offset(5)
        }
    }
}