tos*_*osi 1 uipickerview nsattributedstring ios swift
我需要一个带有一些条目的选择器视图,它具有正常的字体粗细,一些具有粗体字体重.找出如何创建一个属性字符串不是问题,所以我把所有东西放在一起,但我看不出有什么区别.第一行不是粗体而不是更大.不过,不同的颜色确实有用.
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let darkColor = UIColor.blackColor()
let lightColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
if row == 0 {
return NSAttributedString(string: "first row bold and black", attributes: [NSForegroundColorAttributeName:darkColor, NSFontAttributeName : UIFont.boldSystemFontOfSize(20)])
} else {
return NSAttributedString(string: "other rows gray and normal", attributes: [NSForegroundColorAttributeName:lightColor])
}
}
Run Code Online (Sandbox Code Playgroud)
使用viewForRow而不是attributionTitleForRow,您可以获得更多控制权
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
let pickerLabel = UILabel()
if row == 0
{
pickerLabel.text = "first row bold and black"
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.font = UIFont.boldSystemFontOfSize(20)
}
else
{
pickerLabel.text = "other rows gray and normal"
pickerLabel.textColor = UIColor.grayColor()
}
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1631 次 |
| 最近记录: |