Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create CheckBox #65

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions GongGanGam-UI/Sources/CheckBox/CheckBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// CheckBox.swift
// GongGanGam
//
// Created by 김세영 on 2023/01/28.
// Copyright © 2023 GongGanGam. All rights reserved.
//

import UIKit
import FlexLayout
import PinLayout

open class CheckBox: UIButton {

// MARK: - UI
private lazy var descriptionLabel: UILabel = {
let label = UILabel()

label.font = Pretendard.body
label.textColor = GongGanGamUIAsset.neutral30.color
return label
}()

private lazy var checkBoxImageView: UIImageView = {
let imageView = UIImageView()

imageView.image = GongGanGamUIAsset.checkOff.image
imageView.contentMode = .scaleAspectFit
return imageView
}()

// MARK: - Properties
private let flexContainer = UIView()

/// `CheckBox`의 선택 여부를 나타냅니다.
public var isChecked: Bool = false {
didSet {
updateState()
}
}

// MARK: - Initializers
public init(title: String) {
super.init(frame: .zero)

descriptionLabel.text = title
self.addTarget(self, action: #selector(checkBoxTapped), for: .touchUpInside)

self.addSubview(flexContainer)
flexContainer.isUserInteractionEnabled = false
flexContainer.flex.direction(.row).define { flex in
flex.addItem(descriptionLabel)
flex.addItem(checkBoxImageView).marginLeft(4).aspectRatio(1).width(24)
}
}

public required init?(coder: NSCoder) {
fatalError("init?(coder:) is called.")
}

// MARK: - Methods
open override func layoutSubviews() {
super.layoutSubviews()

flexContainer.pin.all()
flexContainer.flex.layout()
}

/// Only `isChecked didSet` can call this method.
private func updateState() {
let image = isChecked ? GongGanGamUIAsset.checkOn : GongGanGamUIAsset.checkOff

checkBoxImageView.image = image.image
}

@objc private func checkBoxTapped() {
isChecked.toggle()
}
}
66 changes: 66 additions & 0 deletions GongGanGam-UI/Sources/CheckBox/CheckBoxExampleViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// CheckBoxExampleViewController.swift
// GongGanGam-UI
//
// Created by 김세영 on 2023/01/28.
// Copyright © 2023 GongGanGam. All rights reserved.
//

import UIKit
import FlexLayout
import PinLayout

final class CheckBoxExampleViewController: UIViewController {

// MARK: - UI
private lazy var checkBox: CheckBox = {
let checkBox = CheckBox(title: "일기 공유하기")

return checkBox
}()

private lazy var checkedStatusLabel: UILabel = {
let label = UILabel()

label.text = "press checkBox"
return label
}()

// MARK: - Properties
private let flexContainer = UIView()

// MARK: - Initializers
init() {
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init?(coder:) is called.")
}

// MARK: - Methods
override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = GongGanGamUIAsset.background.color
self.view.addSubview(flexContainer)
flexContainer.flex.alignItems(.center).define { flex in
flex.addItem(checkBox)
flex.addItem(checkedStatusLabel).marginTop(32)
}

checkBox.addTarget(self, action: #selector(checkBoxTapped), for: .touchUpInside)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

flexContainer.pin.all(self.view.pin.safeArea)
flexContainer.flex.layout()
}

@objc func checkBoxTapped(_ sender: CheckBox) {
checkedStatusLabel.text = "isChecked: \(sender.isChecked)"
checkedStatusLabel.sizeToFit()
}
}
5 changes: 5 additions & 0 deletions GongGanGam-UI/Sources/Example/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ internal enum Components: String, CaseIterable {
case titledView
case alertBuilder

case checkbox

var viewController: UIViewController {
switch self {
case .titledView:
return TitledViewExampleViewController()
case.alertBuilder:
return AlertBuilderExampleViewController()

case .checkbox:
return CheckBoxExampleViewController()
}
}
}