1
使用NSAttributedString我pickerView尝试改变字体:设置字体使用NSAttributedString
public func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
guard let castDict = self.castDict else {
return nil
}
let name = [String](castDict.keys)[row]
switch component {
case 0:
return NSAttributedString(string: name, attributes: [NSForegroundColorAttributeName : AppColors.Rose.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
case 1:
guard let character = castDict[name] else {
return NSAttributedString(string: "Not found character for \(name)", attributes: [NSForegroundColorAttributeName : AppColors.Rose.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
}
return NSAttributedString(string: character, attributes: [NSForegroundColorAttributeName : AppColors.LightBlue.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
default:
return nil
}
}
颜色改变,字体 - 不是:
我做错了吗?
感谢详细的解答。是的,它使用自定义视图。出于性能原因,请始终不要使用视图 – zzheads