iOS 개발/번역기 앱(RxSwift)

번역기 앱 - 6. 보관함 화면 Cell 구현

Skillist 2022. 2. 1. 16:26
반응형

안녕하세요. Skillist입니다

 

눈이 오고 난 후에 미세먼지가 많아져서 기분 또한 다운됐네요.

 

이번엔 Cell을 살펴볼게요.

기록 화면의 Cell과 거의 유사하기때문에, pass하셔도 됩니다.

 

설명은 쭉쭉 넘어갈게요

 

———————————————————————————————————————————————————

 

Cell에서 사용할 UI 컴포넌트들입니다.

 

UI컴포넌트들과 이니셜라이저입니다.

 

스냅킷으로 제약사항을 구성했습니다.

 

스냅킷으로 제약사항을 구성했습니다.

addTarget에서 사용할, func입니다. private extension으로 따로 구현했습니다.

 

———————————————————————————————————————————————————

 

이번은 따로할 코멘트가 없었어요.

cell은 스냅킷 구현밖에 없었기에 패스했습니다.

 

잘못되거나 부족한 내용 등, 피드백 감사합니다!

 

https://github.com/DeveloperSkillist/TranstorKing

 

GitHub - DeveloperSkillist/TranstorKing

Contribute to DeveloperSkillist/TranstorKing development by creating an account on GitHub.

github.com

 

 

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓  전체 코드  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

import UIKit

class BookMarkTableViewCell: UITableViewCell {
    static let identify = "BookMarkTableViewCell"
    var bookmark: HistoryModel?
    
    private var uiView: UIView = {
        let uiView = UIView()
        uiView.backgroundColor = .systemBackground
        uiView.layer.cornerRadius = 10
        uiView.clipsToBounds = true
        return uiView
    }()
    
    private var sourceLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 17, weight: .bold)
        label.textColor = .systemGray
        label.text = Language.ko.title
        label.numberOfLines = 1
        return label
    }()
    
    private lazy var copyButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(systemName: "doc.on.doc"), for: .normal)
        button.tintColor = .systemGray
        button.addTarget(self, action: #selector(copyBookmark), for: .touchUpInside)
        return button
    }()
    
    private lazy var clearButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(systemName: "xmark"), for: .normal)
        button.tintColor = .systemGray
        button.addTarget(self, action: #selector(deleteBookmark), for: .touchUpInside)
        return button
    }()
    
    private lazy var sourceTextLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .systemBackground
        label.font = .systemFont(ofSize: 20)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var lineView: UIView = {
        var view = UIView()
        view.backgroundColor = .secondarySystemBackground
        return view
    }()
    
    private lazy var targetLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 17, weight: .bold)
        label.textColor = .systemGray
        label.text = Language.en.title
        label.numberOfLines = 1
        return label
    }()
    
    private lazy var translatedTextLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .systemBackground
        label.font = .systemFont(ofSize: 20)
        label.numberOfLines = 0
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        attribute()
        layout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func attribute() {
        self.backgroundColor = .secondarySystemBackground
    }
    
    private func layout() {
        
        contentView.addSubview(uiView)
        uiView.snp.makeConstraints {
            $0.top.bottom.equalToSuperview().inset(8)
            $0.leading.trailing.equalToSuperview().inset(16)
        }
        
        [
            sourceLabel,
            copyButton,
            clearButton,
            sourceTextLabel,
            lineView,
            targetLabel,
            translatedTextLabel
        ].forEach {
            uiView.addSubview($0)
        }
        
        sourceLabel.snp.makeConstraints {
            $0.top.leading.equalToSuperview().inset(16)
            $0.trailing.equalTo(copyButton.snp.leading).offset(-16)
        }
  
        clearButton.snp.makeConstraints {
            $0.top.bottom.equalTo(sourceLabel)
            $0.trailing.equalToSuperview().inset(16)
            $0.width.height.equalTo(sourceLabel.snp.height)
        }
        
        copyButton.snp.makeConstraints {
            $0.top.bottom.equalTo(sourceLabel)
            $0.trailing.equalTo(clearButton.snp.leading).offset(-8)
            $0.width.height.equalTo(sourceLabel.snp.height)
        }
        
        sourceTextLabel.snp.makeConstraints {
            $0.top.equalTo(sourceLabel.snp.bottom).offset(8)
            $0.leading.equalTo(sourceLabel)
            $0.trailing.equalTo(clearButton)
        }
        
        lineView.snp.makeConstraints {
            $0.top.equalTo(sourceTextLabel.snp.bottom).offset(16)
            $0.leading.trailing.equalToSuperview()
            $0.height.equalTo(1)
        }
        
        targetLabel.snp.makeConstraints {
            $0.top.equalTo(lineView.snp.bottom).offset(16)
            $0.leading.equalTo(sourceLabel)
            $0.trailing.equalTo(clearButton)
        }
        
        translatedTextLabel.snp.makeConstraints {
            $0.top.equalTo(targetLabel.snp.bottom).offset(8)
            $0.leading.equalTo(sourceLabel)
            $0.trailing.equalTo(clearButton)
            $0.bottom.equalToSuperview().inset(16)
        }
    }
    
    func setup(bookmark: HistoryModel) {
        self.bookmark = bookmark
        sourceLabel.text = bookmark.sourceLanguage.title
        sourceTextLabel.text = bookmark.sourceText
        targetLabel.text = bookmark.targetLanguage.title
        translatedTextLabel.text = bookmark.targetText
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
    }
}

private extension BookMarkTableViewCell {
    @objc func copyBookmark() {
        guard let bookmark = bookmark else {
            return
        }
        
        UIPasteboard.general.string = bookmark.targetText
    }
    
    @objc func deleteBookmark() {
        guard let bookmark = bookmark else {
            return
        }
        
        UserDefaults.standard.deleteBookmark(historyModel: bookmark)
    }
}
반응형