Swift Lover
Swift Lover

Reputation: 21

PickerView fontsize change accordingly iOS Swift

should look like this In pickerView i want to change title font size, as I scroll the selected row title font will be of high font size and above selected title font size will be decreasing and same for below selected row.

 import UIKit

Vc:

class ViewController: UIViewController {
    @IBOutlet weak var pickerView: UIPickerView!
    var pickerTitle = ["10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00","10:00"]. // time to display in pickerView

var fontSize = [4,6,8,10,12,14,16,18,20,22,24,26,24,22,20,18,16,14,12,10,8,6,4,2] // font size when view is loaded and when I scroll I need to change the font size so this won't work I guess
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    pickerView.delegate = self
    pickerView.dataSource = self

    pickerView.reloadAllComponents()
    pickerView.selectRow(11, inComponent: 0, animated: true)
}
}

datasource and delete for pickerView:

 extension ViewController: UIPickerViewDelegate,UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerTitle.count
}


func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    let titleData = pickerTitle[row]

    let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: CGFloat(fontSize[row]))!,NSAttributedString.Key.foregroundColor:UIColor.black])
    pickerLabel.attributedText = myTitle

    pickerLabel.textAlignment = .right
        return pickerLabel
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    for var i in 0...row{
        print(i)
    }

    for var i in row..<pickerTitle.count{
        print(i)
    }
    pickerView.reloadAllComponents()
}   
}

Upvotes: 2

Views: 1251

Answers (1)

Mumtaz Hussain
Mumtaz Hussain

Reputation: 1125

Not sure what your requirements are, and what you're doing in the code. But what I could understand is that you want an increased font size for selected title in pickerView, and the rest unselected titles stays smaller; then here is the code: (if not, please clarify further)

extension ViewController: UIPickerViewDelegate,UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerTitle.count
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerView.reloadAllComponents()
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        var label = view as! UILabel?
        if label == nil {
            label = UILabel()
            label?.textAlignment = .center
        }
        switch component {
        case 0:

            label?.text = pickerTitle[row]
            if(pickerView.selectedRow(inComponent: 0) == row){
                label?.font = UIFont(name: "Georgia", size: 26)
            }
            return label!

        default:
            return label!
        }

    }
}

Upvotes: 1

Related Questions