当我在attributedTitleForRow
中更改字体颜色和大小时,字体大小不会更改,但所选行颜色会更改:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var color: UIColor!
var size: UIFont!
switch component {
case 0:
if pickerView.selectedRow(inComponent: 0) == row {
color = UIColor.init(red: 186/255, green: 61/255, blue: 62/255, alpha: 1.0)
size = UIFont(name:"Raleway", size:14)
} else {
color = UIColor.black
}
let attributes: [String: AnyObject] = [
NSForegroundColorAttributeName: color, NSFontAttributeName: size]
return NSAttributedString(string: peopleCount[row], attributes: attributes)
default:
return NSAttributedString()
}
}
当我在viewForRow
方法中更改字体颜色和大小时,所有行的颜色和字体大小都会发生变化:
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?.textColor = UIColor.red
}
switch component {
case 0:
label?.text = peopleCount[row]
label?.font = UIFont(name:"Raleway", size:14)
return label!
default:
return label!
}
}
所以,我的问题是如何更改所有行的字体大小,但颜色只更改选定的行?为:
答案 0 :(得分:0)
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
//label?.textColor = UIColor.red
}
switch component {
case 0:
label?.text = peopleCount[row]
if(pickerView.selectedRow(inComponent: 0)==row){
label?.textColor = UIColor.red
}
return label!
default:
return label!
}
}
您可以像这样编辑代码,并查看标签颜色已更改,您必须在下面添加此代码。这与点击事件
有关 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.reloadAllComponents()
}