[英]How to change font size of UIPickerView component in Swift?
当我在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!
}
}
所以,我的问题是,如何更改所有行的字体大小,但仅更改所选行的颜色? 如:
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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.