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