CNLiveMineNickViewController.swift 4.25 KB
//
//  CNLiveMineNickViewController.swift
//  metaCode
//
//  Created by open on 2023/6/9.
//

import Foundation

class CNLiveMineNickViewController: CNLiveBaseViewController, UITextFieldDelegate {
    
    
    lazy var saveButton: UIButton = {
        let saveButton = UIButton.init(type: .custom)
        saveButton.frame = CGRect(x: KSreenWidth - 70, y: (get_system_nav_height() - (get_system_statusBar_height() + 25))/2 + get_system_statusBar_height(), width: 60, height: 25)
        saveButton.setTitle("保存", for: .normal)
        saveButton.titleLabel?.font = Font_15
        saveButton.setTitleColor(.white, for: .normal)
        saveButton.backgroundColor = HexColor("#EBEBEB")
        saveButton.layer.masksToBounds  = true
        saveButton.layer.cornerRadius = 5
        saveButton.addTarget(self, action: #selector(clickSaveAction), for: .touchUpInside)
        return saveButton
    }()
    
    lazy var mainView: UIView = {
        let mainView = UIView.init()
        mainView.backgroundColor = .white
        mainView.layer.cornerRadius = 8
        mainView.layer.masksToBounds = true
        return mainView
    }()
    
    lazy var nickField: UITextField = {
        let nickFielld = UITextField.init()
        nickFielld.backgroundColor = .white
        nickFielld.placeholder = "输入昵称"
        nickFielld.font = BoldFont_16_Fit
        nickFielld.delegate = self
        nickFielld.text = UserInfoManager.shared.userInfo.nickName
        nickFielld.keyboardType = .default
        nickFielld.clearButtonMode = .whileEditing
        return nickFielld
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        titleName = "更改昵称"
        navigationBarHidden = false
        topNavView.addSubview(saveButton)
        
        view.addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(get_system_nav_height()+10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(50)
        }
        
        mainView.addSubview(nickField)
        nickField.snp.makeConstraints { make in
            make.centerX.equalTo(mainView)
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-5)
        }
        setupWithSendBtnStatue(statue: true)
    }
    
    
    private func setupWithSendBtnStatue(statue: Bool) {
        if statue {
            saveButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 1),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        } else {
            saveButton.setGradient(colors: [UIColor(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1)], startPoint: CGPoint(x: 0, y: 1), endPoint: CGPoint(x: 1, y: 0))
        }
        var color: UIColor = UIColor.white
        if !statue {
            color = HexColor("#999999")!
        }
        saveButton.isUserInteractionEnabled = statue
        saveButton.setTitleColor(color, for: .normal)
    }
    
    
    @objc func clickSaveAction() {
        print("保存")
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == nickField {
            mainView.layer.borderColor = HexColor("#0066FE")?.cgColor
            mainView.layer.borderWidth = 1
            setupWithSendBtnStatue(statue: true)
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == nickField && textField.text?.count == 0 {
            mainView.layer.borderColor = UIColor.white.cgColor
            mainView.layer.borderWidth = 1
            setupWithSendBtnStatue(statue: false)
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        if textField.returnKeyType == .done {
            
            if string == "\n" {
                
                textField.resignFirstResponder()
            }
            return true
        }
        return true
    }
}