[TIL] 파이널 프로젝트 5일자

7과11사이·2023년 10월 16일
0

스파르타코딩클럽

목록 보기
79/90

기술적 고민

Custom code or Factory

로그인 / 회원가입 화면을 구현하면서 반복되는 코드를 마주하게 됐다.
로그인 페이지인만큼 값을 입력할 수 있는 textFieldView가 자주 사용되었는데, 이를 factory를 하여 코드를 만들어야 할지 - Custom 타입을 만들어야 하는지 궁금해졌다.

초반 Factory 구성

ComponentFactory {
	func createTextfield(with placeholder: String) -> UITextField {
		let tf = UITextField()
		tf.placeholder = placeholder
		tf.keyboardType = .default
		tf.textColor = .black
	}

	func addImage(withImage image: String) -> UIImageView {
	    let iv = UIImageView()
	    let image = UIImage(named: image)
	    iv.image = image
	    iv.translatesAutoresizingMaskIntoConstraints = false
	    return iv
	}
	
	func makeLabel(withText text: String, size: CGFloat) -> UILabel {
	    let label = UILabel()
	    label.text = text
	    label.textColor = .white
	    label.font = UIFont.systemFont(ofSize: size)
	    label.shadowColor = .black
	    label.shadowOffset = CGSize(width: 0, height: -1)
	    label.translatesAutoresizingMaskIntoConstraints = false
	    return label
	}
	
	func makeButton(withImage name: String, action: Selector, target: Any) -> UIButton {
	    let button = UIButton()
	    let image = UIImage(systemName: name)?.withTintColor(.white, renderingMode: .alwaysOriginal)
	    button.setImage(image, for: .normal)
	    button.addTarget(target, action: action, for: .touchUpInside)
	    button.translatesAutoresizingMaskIntoConstraints = false
	    return button
	}
...
}

Custom Component 구현

class CustomTextfieldView: UIView {
    
	enum ButtonType {
		case noButton
		case cancelButton
		case hideButton
		case checkButton
		case crossCheckButton
	}

	init(placeholder: String, text: String) {
		super.init(frame: .zero)
		textfield.placeholder = placeholder
		animatingLabel.text = text
	        
		configure()
		setUI()
		commonInit()
	}

	init(placeholder: String, text: String, alertMessage: String, button: ButtonType) {
		super.init(frame: .zero)
		textfield.placeholder = placeholder
		animatingLabel.text = text
		validationLabel.text = alertMessage
     
		configure()
		setUI()
		commonInit()
	        
		switch button {
			case .noButton: print("no button")
			case .cancelButton: addCancelButton()
			case .hideButton: addEyeButton()
			case .checkButton: addCheckButton()
			case .crossCheckButton: addCrossCheckButton()
		}
		
		private func commonInit() {
			secureButton.addTarget(self, action: #selector(secureButtonTapped), for: .touchUpInside)
			let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
			addGestureRecognizer(tap)
			self.isUserInteractionEnabled = true
    }

    override var intrinsicContentSize: CGSize {
			return CGSize(width: UIView.noIntrinsicMetric, height: 55)
    }
}

0개의 댓글