UIPickerView

时间:2015-07-26 04:25:21

标签: ios xcode swift

在UIPickerView中,是否可以在下面的图片中添加我编辑的列标题?另外,如何保持对齐UIPickerView文本以使所有标题都适合?

4 个答案:

答案 0 :(得分:0)

可能就像在UIPickerView上放置UILabel一样简单。如果它与您的内容重叠,请尝试在每个数字的右侧添加足够的空格,将其推向左侧。

答案 1 :(得分:0)

我这样做的方法是添加6个组件, 如果component为0,则从Num [0,1,2,3]等中获取数字 如果component为1,则从label [0](这是文本的数组,label [Hour,Min,Sec])获取

因此组件3将为label [1],依此类推

var mesurFTList = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
var mesurInchList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
var heightList = ["ft", "in"]

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    if component == 0 {
        let str = mesurFTList[row]
        pickerView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
        return NSAttributedString(string: str, attributes: [NSAttributedString.Key.foregroundColor:UIColor.white])
    }else {
        if component == 1 {
            let str = heightList[0]
            pickerView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
            return NSAttributedString(string: str, attributes: [NSAttributedString.Key.foregroundColor:UIColor.white])
        }else {
            if component == 2 {
                let str = mesurInchList[row]
                pickerView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
                return NSAttributedString(string: str, attributes: [NSAttributedString.Key.foregroundColor:UIColor.white])
            }else {
                let str = heightList[1]
                pickerView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
                return NSAttributedString(string: str, attributes: [NSAttributedString.Key.foregroundColor:UIColor.white])
            }
        }
    }

}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 {
        return mesurFTList.count
    }else {
        if component == 1 {
            return 1
        }else {
            if component == 2 {
                return mesurInchList.count
            }else {
                if component == 3 {
                    return 1
                }
            }
        }
    }
    return 1
}

答案 2 :(得分:0)

let pickerView = UIPickerView(frame: CGRect(x: 0, y: 50, width: 260, height: 162))
let hoursLabel = UILabel()
hoursLabel.text = "Hours"
hoursLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
hoursLabel.sizeToFit()
hoursLabel.frame = CGRect(x: pickerView.frame.width * 0.30, y: pickerView.frame.height * 0.5 - (hoursLabel.frame.height / 2.0), width: hoursLabel.frame.width, height: hoursLabel.frame.height)
        pickerView.addSubview(hoursLabel)

let minLabel = UILabel()
minLabel.text = "min"
minLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
minLabel.sizeToFit()
minLabel.frame = CGRect(x: pickerView.frame.width * 0.70, y: pickerView.frame.height * 0.5 - (minLabel.frame.height / 2.0), width: minLabel.frame.width, height: minLabel.frame.height)
pickerView.addSubview(minLabel)

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0
    {
        return "\(hours[row])\t\t"
    }
    else if component == 1
    {
        return "\(mins[row])\t\t\t"
    }
    return "?"
}

答案 3 :(得分:0)

我认为最不容易破解的方法是在选择器中格式化数字以包含所需标签。这与您在图像中所指示的不完全相同,但是对于用户而言,这将导致相同的清晰度。