ModifyGenderVC.swift 3.09 KB
//
//  ModifyGenderVC.swift
//  Twear
//
//  Created by yangbin on 2021/12/27.
//

import UIKit

class ModifyGenderVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    @IBOutlet weak var pickerView: UIPickerView!
    
    var selectedRow: Int = 0
    var genderClosure: ((_ gender: Int) -> ())?
    let genderArray = ["♂ \(LocString("男"))", "♀ \(LocString("女"))"]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.selectRow(selectedRow, inComponent: 0, animated: false)
        // Do any additional setup after loading the view.
    }
    
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return genderArray.count
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return genderArray[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        var pickerLabel = view as? UILabel
        if pickerLabel == nil {
            pickerLabel = UILabel()
            pickerLabel?.font = BoldFont(20)
            pickerLabel?.textAlignment = .center
            pickerLabel?.textColor = UIColor.lightGray
        }
        if row == selectedRow {
            pickerLabel?.attributedText = self.pickerView(pickerView, attributedTitleForRow: selectedRow, forComponent: component)
        } else {
            pickerLabel?.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
        }
        return pickerLabel!
        
    }
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let string = NSMutableAttributedString(string: genderArray[row])
        string.addAttributes([.foregroundColor: UIColor.rgbColorFromHex(0x00993E), .font: BoldFont(25)], range: NSMakeRange(0, string.string.length))
        
        return string
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedRow = row
        pickerView.reloadAllComponents()
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 45
    }
    
    @IBAction func saveGender(_ sender: Any) {
        genderClosure?(1-selectedRow)
        navigationController?.popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}