비전공자 개발일기

Display Background color & Text Setting 본문

SWIFT

Display Background color & Text Setting

HiroDaegu 2022. 12. 13. 19:54
728x90
SMALL

배경색 및 글자색 변경 어플, 설정에서 바꾼 것으로 적용되며, 다시 설정으로 돌아오면 입력 및 선택한 값 유지


import UIKit

class ViewController: UIViewController, displaySettingDelegate {

    @IBOutlet weak var contentLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        contentLabel.textColor = UIColor.orange
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let settingViewController = segue.destination as? SettingViewController {
            settingViewController.delegate = self
            settingViewController.displayText = contentLabel.text
            settingViewController.textColor = contentLabel.textColor
            settingViewController.backgroundColor = view.backgroundColor ?? .black
        }
    }

    func changeSetting(text: String?, textColor: UIColor, backgroundColor: UIColor) {
        if let text = text {
            contentLabel.text = text
        }
        contentLabel.textColor = textColor
        view.backgroundColor = backgroundColor
    }

}
import UIKit

protocol displaySettingDelegate: AnyObject {
    func changeSetting(text: String?, textColor: UIColor, backgroundColor: UIColor)
}

class SettingViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var blueBTN: UIButton!
    @IBOutlet weak var purpleBTN: UIButton!
    @IBOutlet weak var blackBTN: UIButton!
    @IBOutlet weak var greenBTN: UIButton!
    @IBOutlet weak var yellowBTN: UIButton!
    @IBOutlet weak var orangeBTN: UIButton!
    
    weak var delegate: displaySettingDelegate?
    
    var displayText: String?
    var textColor: UIColor = .orange
    var backgroundColor: UIColor = .black
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
    }
    
    private func makeUI() {
        if let displayText = self.displayText {
            textField.text = displayText
        }
        changeTextClr(color: textColor)
        changeBgClr(color: backgroundColor)
    }
    
    
    @IBAction func changeTextColor(_ sender: UIButton) {
        if sender == orangeBTN {
            changeTextClr(color: .orange)
            textColor = .orange
        } else if sender == yellowBTN {
            changeTextClr(color: .yellow)
            textColor = .yellow
        } else {
            changeTextClr(color: .green)
            textColor = .green
        }
    }

    @IBAction func changeBGColor(_ sender: UIButton) {
        if sender == blackBTN {
            changeBgClr(color: .black)
            backgroundColor = .black
        } else if sender == purpleBTN {
            changeBgClr(color: .purple)
            backgroundColor = .purple
        } else {
            changeBgClr(color: .blue)
            backgroundColor = .blue
        }
    }
    
    @IBAction func saveBTN(_ sender: UIButton) {
        delegate?.changeSetting(text: textField.text, textColor: textColor, backgroundColor: backgroundColor)
        navigationController?.popViewController(animated: true)
    }
    
    private func changeTextClr(color: UIColor) {
        orangeBTN.alpha = color == UIColor.orange ? 1 : 0.2
        yellowBTN.alpha = color == UIColor.yellow ? 1 : 0.2
        greenBTN.alpha = color == UIColor.green ? 1 : 0.2
    }
    
    private func changeBgClr(color: UIColor) {
        blackBTN.alpha = color == UIColor.black ? 1 : 0.2
        purpleBTN.alpha = color == UIColor.purple ? 1 : 0.2
        blueBTN.alpha = color == UIColor.blue ? 1 : 0.2
    }
}
728x90
LIST

'SWIFT' 카테고리의 다른 글

To Do List  (0) 2022.12.14
Basic Calculator  (0) 2022.12.13
Quote Generator  (0) 2022.12.12
Swift Study - Function, Class, Struct, Closure  (0) 2022.12.10
BMI Calculator  (0) 2022.12.08