swift UIPickerView 更改背景色,隐藏分割线,修改字体的font和颜色

 

 

        //隐藏中间两条分割线
        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
    }

 

你可能感兴趣的:(swift UIPickerView 更改背景色,隐藏分割线,修改字体的font和颜色)