iOS) UIPickerView 에 고정 라벨 추가하기
less than 1 minute read
extension UIPickerView {
func setPickerLabelsWith(labels: [String]) {
let columCount = labels.count
let fontSize: CGFloat = UIFont.textBold01.pointSize + 3
var labelList: [UILabel] = []
for index in 0..<columCount {
let label = UILabel()
label.text = labels[index]
label.font = .textBold01
label.textColor = .mainColorNadaMain
label.sizeToFit()
labelList.append(label)
}
let pickerWidth: CGFloat = self.frame.width
let labelY: CGFloat = (self.frame.size.height / 2) - (fontSize / 2)
for (index, label) in labelList.enumerated() {
let labelX: CGFloat = (pickerWidth / CGFloat(columCount)) * CGFloat(index + 1) - fontSize
label.frame = CGRect(x: labelX, y: labelY, width: fontSize, height: fontSize)
self.addSubview(label)
}
}
}
// 사용
// pickerView.setPickerLabelsWith(labels: ["년","월","일"])