From dc39a8bf0a17192300b10cdcedaed7579603300c Mon Sep 17 00:00:00 2001 From: hyj422 Date: Thu, 13 Jul 2023 05:16:31 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]:CardWish,=20CardCoupon,=20Textfield=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SDSKit/Component/Card/SDSCardWish.swift | 91 ++++++++ .../Component/Card/SDSCardWishCoupon.swift | 194 ++++++++++++++++++ .../Component/Card/SDSCardWishType.swift | 13 ++ .../Component/Textfield/SDSTextfield.swift | 103 ++++++++++ 4 files changed, 401 insertions(+) create mode 100644 Sources/SDSKit/Component/Card/SDSCardWish.swift create mode 100644 Sources/SDSKit/Component/Card/SDSCardWishCoupon.swift create mode 100644 Sources/SDSKit/Component/Card/SDSCardWishType.swift create mode 100644 Sources/SDSKit/Component/Textfield/SDSTextfield.swift diff --git a/Sources/SDSKit/Component/Card/SDSCardWish.swift b/Sources/SDSKit/Component/Card/SDSCardWish.swift new file mode 100644 index 0000000..61a7753 --- /dev/null +++ b/Sources/SDSKit/Component/Card/SDSCardWish.swift @@ -0,0 +1,91 @@ +// +// File.swift +// +// +// Created by 홍유정 on 2023/07/13. +// + +import UIKit +import SnapKit + +public class SDSCardWish: UIView { + + let wishBackgroundView: UIView = { + let view = UIView() + view.backgroundColor = .gray000 + view.layer.cornerRadius = 10 + return view + }() + + let wishImageView: UIImageView = { + let image = UIImageView() + image.backgroundColor = .gray100 + return image + }() + + let wishTitleLabel: UILabel = { + let label = UILabel() + label.font = SDSFont.body2.font + label.textColor = .gray600 + return label + }() + + public init(title: String, type: SDSCardWishType) { + super.init(frame: .zero) + setLayout() + setType(title: title, type: type) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setLayout() { + addSubview(wishBackgroundView) + [wishImageView, wishTitleLabel] .forEach{wishBackgroundView.addSubview($0)} + + wishBackgroundView.snp.makeConstraints { + $0.edges.equalToSuperview() + } + wishImageView.snp.makeConstraints { + $0.centerX.equalToSuperview() + } + wishTitleLabel.snp.makeConstraints { + $0.centerX.equalToSuperview() + } + } + + func setType(title: String, type: SDSCardWishType) { + switch type{ + case .title: + + self.snp.makeConstraints { + $0.width.equalTo(160) + $0.height.equalTo(206) + } + + wishImageView.snp.makeConstraints { + $0.width.height.equalTo(80) + $0.top.equalToSuperview().offset(28) + } + wishTitleLabel.text = title + wishTitleLabel.isHidden = false + wishTitleLabel.snp.makeConstraints { + $0.top.equalTo(wishImageView.snp.bottom).offset(8) + $0.height.equalTo(40) + } + case .noTitle: + + self.snp.makeConstraints { + $0.width.equalTo(160) + $0.height.equalTo(122) + } + + wishImageView.snp.makeConstraints { + $0.width.height.equalTo(40) + $0.top.equalToSuperview().offset(24) + } + wishTitleLabel.isHidden = true + } + } +} diff --git a/Sources/SDSKit/Component/Card/SDSCardWishCoupon.swift b/Sources/SDSKit/Component/Card/SDSCardWishCoupon.swift new file mode 100644 index 0000000..9a68da3 --- /dev/null +++ b/Sources/SDSKit/Component/Card/SDSCardWishCoupon.swift @@ -0,0 +1,194 @@ +// +// File.swift +// +// +// Created by 홍유정 on 2023/07/11. +// + +import SnapKit +import UIKit + +public class SDSCardWishCoupon: UIView { + + let wishCouponPlaceholder: String = "이번 승부에 걸 소원을 입력해주세요!\n소원은 지금 정하지 않고 승부가 끝난 뒤\n사용할 수도 있어요" + + let wishCouponBackgroundView: UIView = { + let view = UIView() + view.backgroundColor = .gray000 + view.layer.cornerRadius = 6 + return view + }() + + let wishCouponTextBackgroundView: UIView = { + let view = UIView() + view.backgroundColor = .gray100 + view.layer.cornerRadius = 6 + return view + }() + + lazy var wishCouponTextView: UITextView = { + let textview = UITextView() + textview.font = SDSFont.body2.font + textview.backgroundColor = .clear + textview.textContainerInset = .zero + return textview + }() + + let dashlineStackView: UIStackView = { + let stackView = UIStackView() + stackView.axis = .vertical + stackView.distribution = .fillEqually + stackView.spacing = 8 + stackView.backgroundColor = .gray000 + return stackView + }() + + let dashView: UIView = { + let view = UIView() + view.backgroundColor = .blue + return view + }() + + lazy var wishCountLabel: UILabel = { + let label = UILabel() + label.text = "0/54" + label.textColor = .gray200 + label.font = SDSFont.caption.font + return label + }() + + func setTextView() { + wishCouponTextView.delegate = self + wishCouponTextView.text = wishCouponPlaceholder + let style = NSMutableParagraphStyle() + style.lineSpacing = 5 + wishCouponTextView.attributedText = NSAttributedString(string: wishCouponTextView.text, attributes: [NSAttributedString.Key.paragraphStyle: style]) + wishCouponTextView.textColor = .gray300 + } + + func maskCoupon() { + let circleSize = 20 + let maskLayer = CAShapeLayer() + let backgroundPath = UIBezierPath(rect: wishCouponBackgroundView.bounds) + let transparentLeftPath = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x: -10, y: 46), size: CGSize(width: circleSize, height: circleSize)), cornerRadius: CGFloat(circleSize/2)) + let transparentRightPath = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x: wishCouponBackgroundView.frame.maxX - 10, y: 46), size: CGSize(width: circleSize, height: circleSize)), cornerRadius: CGFloat(circleSize/2)) + + + let combinedPath = UIBezierPath() + + combinedPath.append(backgroundPath) + combinedPath.append(transparentLeftPath) + combinedPath.append(transparentRightPath) + + maskLayer.fillRule = .evenOdd + maskLayer.path = combinedPath.cgPath + wishCouponBackgroundView.layer.mask = maskLayer + } + + public override func layoutSubviews() { + super.layoutSubviews() + maskCoupon() + } + + public override func touchesBegan(_ touches: Set, with event: UIEvent?) { + self.endEditing(true) + } + + public init() { + super.init(frame: .zero) + setLayout() + setTextView() + self.clipsToBounds = true + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setLayout() { + + addSubview(wishCouponBackgroundView) + [wishCouponTextBackgroundView, dashlineStackView, wishCountLabel].forEach { wishCouponBackgroundView.addSubview($0)} + wishCouponTextBackgroundView.addSubview(wishCouponTextView) + + for _ in 0...6 { + let verticalView = UIView() + verticalView.backgroundColor = .gray100 + self.dashlineStackView.addArrangedSubview(verticalView) + } + + self.snp.makeConstraints { + $0.height.equalTo(112) + $0.width.equalTo(334) + } + wishCouponBackgroundView.snp.makeConstraints { + $0.edges.equalToSuperview() + } + + wishCouponTextBackgroundView.snp.makeConstraints { + $0.top.bottom.equalToSuperview().inset(16) + $0.leading.equalToSuperview().offset(19) + $0.trailing.equalToSuperview().inset(77) + $0.height.equalTo(80) + } + wishCouponTextView.snp.makeConstraints { + $0.top.bottom.leading.trailing.equalToSuperview().inset(10) + } + + dashlineStackView.snp.makeConstraints { + $0.top.bottom.equalToSuperview() + $0.top.bottom.equalToSuperview() + $0.leading.equalTo(wishCouponTextBackgroundView.snp.trailing).offset(16) + $0.width.equalTo(5) + } + + wishCountLabel.snp.makeConstraints { + $0.leading.equalTo(dashlineStackView.snp.trailing).offset(11) + $0.bottom.equalToSuperview().inset(11) + } + } +} + +extension SDSCardWishCoupon: UITextViewDelegate { + + public func textViewDidBeginEditing(_ textView: UITextView) { + wishCouponTextBackgroundView.layer.borderWidth = 1 + wishCouponTextBackgroundView.layer.borderColor = UIColor.lightBlue500.cgColor + + if textView.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + wishCouponTextView.textColor = .gray300 + wishCouponTextView.text = wishCouponPlaceholder + } else if textView.text == wishCouponPlaceholder { + wishCouponTextView.textColor = .gray600 + wishCouponTextView.text = "" + } + } + + public func textViewDidChange(_ textView: UITextView) { + + let style = NSMutableParagraphStyle() + style.lineSpacing = 5 + textView.attributedText = NSAttributedString(string: textView.text, attributes: [NSAttributedString.Key.paragraphStyle: style]) + + if wishCouponTextView.text.count > 54 { + wishCouponTextView.deleteBackward() + } else if wishCouponTextView.text.count == 54 { + wishCouponTextBackgroundView.layer.borderColor = UIColor.red500.cgColor + wishCountLabel.textColor = .red500 + } + else { + wishCouponTextView.textColor = .gray600 + wishCountLabel.textColor = .gray200 + wishCouponTextBackgroundView.layer.borderColor = UIColor.lightBlue500.cgColor + } + wishCountLabel.text = "\(wishCouponTextView.text.count)/54" + } + + public func textViewDidEndEditing(_ textView: UITextView) { + wishCouponTextBackgroundView.layer.borderWidth = 0 + if wishCouponTextView.text.isEmpty { + wishCouponTextView.text = wishCouponPlaceholder + wishCouponTextView.textColor = .gray300 + } + } +} diff --git a/Sources/SDSKit/Component/Card/SDSCardWishType.swift b/Sources/SDSKit/Component/Card/SDSCardWishType.swift new file mode 100644 index 0000000..8b3578a --- /dev/null +++ b/Sources/SDSKit/Component/Card/SDSCardWishType.swift @@ -0,0 +1,13 @@ +// +// File.swift +// +// +// Created by 홍유정 on 2023/07/13. +// + +import Foundation + +public enum SDSCardWishType { + case title + case noTitle +} diff --git a/Sources/SDSKit/Component/Textfield/SDSTextfield.swift b/Sources/SDSKit/Component/Textfield/SDSTextfield.swift new file mode 100644 index 0000000..0d38831 --- /dev/null +++ b/Sources/SDSKit/Component/Textfield/SDSTextfield.swift @@ -0,0 +1,103 @@ +// +// File.swift +// +// +// Created by 홍유정 on 2023/07/13. +// + +import UIKit +import SnapKit + +public class SDSTextfield: UIView { + + lazy var sdsTextfield: UITextField = { + let textfield = UITextField() + textfield.layer.cornerRadius = 10 + textfield.font = SDSFont.body2.font + textfield.backgroundColor = .gray000 + textfield.leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 15.0, height: 0.0)) //왼쪽 여백 주기 + textfield.leftViewMode = .always + textfield.addTarget(self, action: #selector(self.textfieldDidChange(_:)), for: .editingChanged) + return textfield + }() + + lazy var textfieldCountLabel: UILabel = { + let label = UILabel() + label.text = "0/10" + label.textColor = .gray400 + label.font = SDSFont.caption.font + return label + }() + + lazy var errorLabel: UILabel = { + let label = UILabel() + label.text = "Error message" + label.font = SDSFont.caption.font + label.textColor = .red500 + label.isHidden = true + return label + }() + + public init(placeholder: String) { + super.init(frame: .zero) + sdsTextfield.delegate = self + sdsTextfield.placeholder = placeholder + setLayout() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func setLayout() { + [sdsTextfield, errorLabel, textfieldCountLabel].forEach{ addSubview($0)} + + self.snp.makeConstraints { + $0.width.equalTo(328) + $0.height.equalTo(48) + } + + sdsTextfield.snp.makeConstraints { + $0.edges.equalToSuperview() + } + + errorLabel.snp.makeConstraints { + $0.leading.equalToSuperview() + $0.top.equalTo(sdsTextfield.snp.bottom).offset(4) + } + textfieldCountLabel.snp.makeConstraints { + $0.top.equalTo(sdsTextfield.snp.bottom).offset(4) + $0.trailing.equalTo(sdsTextfield.snp.trailing) + } + } + + func setTextfieldType() { + + } +} + +extension SDSTextfield: UITextFieldDelegate { + + public func textFieldDidBeginEditing(_ textField: UITextField) { + textField.layer.borderColor = UIColor.lightBlue500.cgColor + textField.layer.borderWidth = 1 + } + + @objc public func textfieldDidChange(_ sender: Any) { + if sdsTextfield.text!.count > 10 { + sdsTextfield.deleteBackward() + sdsTextfield.layer.borderColor = UIColor.red500.cgColor + sdsTextfield.layer.borderWidth = 1 + errorLabel.isHidden = false + } else { + sdsTextfield.layer.borderColor = UIColor.lightBlue500.cgColor + errorLabel.isHidden = true + } + textfieldCountLabel.text = "\(sdsTextfield.text!.count)/10" + } + + public func textFieldDidEndEditing(_ textField: UITextField) { + textField.layer.borderColor = UIColor.gray200.cgColor + textField.layer.borderWidth = 1 + } +}