2015-09-13 6 views
0

Как изменить размер шрифта. Мой код ниже не работает. Я использую xcode7.1 и swift 2 на iOS 9.1.Как изменить размер шрифта UIPickerView

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
    var attributedString: NSMutableAttributedString 
    let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 13.0)! ] 
    switch component { 
    case 0: 
     attributedString = NSMutableAttributedString(string: firstFieldArray[row], attributes: myAttribute) 
    case 1: 
     attributedString = NSMutableAttributedString(string: secondFieldArray[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()]) 
    default: 
     attributedString = NSMutableAttributedString(string: firstFieldArray[row], attributes: myAttribute) 
    } 

    return attributedString 
} 

case 1 работает отлично, а случай 0 не меняется, это шрифт.

ответ

7

NSAttributedString не может быть изменен для UIPickerView.

Вы можете изменить шрифт, перекрывая pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView

+0

Да, я сделал это решение, и это правильно. Кроме того, вы должны добавить UILabel в это представление и установить там свой UIFont. –

0

Swift 4

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 

     var title = UILabel() 
     if let title = view { 
       title = title as! UILabel 
      } 
     title.font = UIFont.systemFont(ofSize: 21, weight: UIFont.Weight.bold) 
     title.textColor = UIColor.blue 
     title.text = PickerArray[row] 
     title.textAlignment = .center 

    return title 

    } 
Смежные вопросы