//隱藏中間兩條分割線 if pickerV.subviews.count >= 2{ pickerV.subviews[1].isHidden = true pickerV.subviews[2].isHidden = true } //修改選中行的背景色 for subView in pickerV.subviews { if subView.subviews.count != 0 { let contentViews = subView.subviews[0] for rowView in contentViews.subviews { if rowView.center.y == contentViews.center.y { //背景view rowView.backgroundColor = UIColor.init(hexString: "#F5F5F5") break } } break } }
// 推薦 顯示需要 label 。文字大小, 顏色等 func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { //顯示的文字 var showStr = "" switch component { case 0: showStr = "\(self.dataArr[component][row].selectTextStr)年" case 1: showStr = "\(self.dataArr[component][row].selectTextStr)月" case 2: showStr = "\(self.dataArr[component][row].selectTextStr)日" default:break } //修改字體大小, 顏色 let arrStr = NSAttributedString.highLightText(showStr, highLightString: "", normalFont: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular), highLightFont: nil, normalColor: UIColor.init(hexString: "#333333"), highLightColor: nil) //這里寬度隨便給的, 高度也是隨便給的 不能比row的高度大,能顯示出來就行 let showLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 34)) showLabel.textAlignment = .center //重新加載label的文字內容 showLabel.attributedText = arrStr return showLabel } // //改變字體顏色,目前字體大小更改不了,不推薦 // func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { // // // var str = "" // switch component { // case 0: // str = "\(self.dataArr[component][row].selectTextStr)年" // case 1: // str = "\(self.dataArr[component][row].selectTextStr)月" // case 2: // str = "\(self.dataArr[component][row].selectTextStr)日" // default:break // } // return NSAttributedString.highLightText(str, highLightString: "", normalFont: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular), highLightFont: nil, normalColor: UIColor.init(hexString: "#333333"), highLightColor: nil) // } //每行的高度 func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 34 }