Cocoa UIPickerView不允许通过代理的“AttributedTitleFrrow”更改字体名称和大小`
我使用以下func更改字体颜色和字体大小,颜色可以工作,但字体名称和字体大小不能工作Cocoa UIPickerView不允许通过代理的“AttributedTitleFrrow”更改字体名称和大小`,cocoa,fonts,size,uipickerview,nsattributedstring,Cocoa,Fonts,Size,Uipickerview,Nsattributedstring,我使用以下func更改字体颜色和字体大小,颜色可以工作,但字体名称和字体大小不能工作 func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttri
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?
var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Arial-BoldMT", size: 45)!, NSForegroundColorAttributeName:UIColor.whiteColor()])
有什么帮助吗
Thx.我还有一个选择,可能对你有帮助 完成正常选取器视图所需的所有工作:获取更多帮助 现在按照这个步骤:-您可以像这样使用viewForRow方法
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = "PickerView Cell Title"
// pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
更新的Swift 3
更新的Swift 5
也许它对您完全有帮助,我在我的项目中也使用了它。您可以为pickerview声明数据源
let arrDataSource:[String] = ["row 1","row 2","row 3"]
然后使用这个字符串数组作为下面函数中行的标题
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
let pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = arrDataSource[row]
pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
//pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
斯威夫特3
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var string = ""
let pickerLabel = UILabel()
pickerLabel.textColor = UIColor.white
pickerLabel.text = string
pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22) // In this use your custom font
pickerLabel.textAlignment = .center
return pickerLabel
}
为了进一步详细说明UILabel的答案,我将使用一种更具内存优化功能的解决方案,它可以重用标签:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel: UILabel
if let label = view as? UILabel {
pickerLabel = label
} else {
pickerLabel = UILabel()
pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22)
pickerLabel.textAlignment = .center
}
pickerLabel.text = pickerData[row] //This is your string
return pickerLabel
}
Swift 4.1/Xcode 9.4.1
显然,您可以选择自己的字体,但这里我使用的是System Bold 13
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.font = UIFont.boldSystemFont(ofSize: 13)
pickerLabel.textColor = UIColor.black
pickerLabel.textAlignment = .center
pickerLabel.text = "PickerView Cell Title"
return pickerLabel
}
是的,你的代码有效。但是我需要从PickerViewEwPickerView、titleForRow:row、forComponent:component获取重新计算的标题数据。顺便问一下,我如何在评论中发布代码呢?pickerView:titleForRow:forComponent:pickerView\UU2AttributedTitleFrow:forComponent:pickerView\U2ViewForRow:forComponent:reusingView:上面三个也可以显示pickerView内容,但不应该同时使用它们,我不知何故使用了其中的两个。现在只使用最后一个,问题就解决了。谢谢大家。这不仅仅是在Swift中…同样的问题在iOS 9 beta 5上仍然无法在iOS 11.4中使用Swift 4.1和Xcode 9.4.1。也许是时候提交一个bug了。iOS 12也不起作用。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel: UILabel
if let label = view as? UILabel {
pickerLabel = label
} else {
pickerLabel = UILabel()
pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22)
pickerLabel.textAlignment = .center
}
pickerLabel.text = pickerData[row] //This is your string
return pickerLabel
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.font = UIFont.boldSystemFont(ofSize: 13)
pickerLabel.textColor = UIColor.black
pickerLabel.textAlignment = .center
pickerLabel.text = "PickerView Cell Title"
return pickerLabel
}