Avec Objective-C, j'ai utilisé le code ci-dessous pour définir/modifier la famille de polices et la taille d'un sélecteur:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
tView.font = [UIFont fontWithName:@"Times New Roman" size:fontsize];;
}
tView.text = [_mysitedata findKeyForRow:row];
NSLog(@"C key:%@ row:%ld comp:%ld", tView.text, (long int)row, (long int)component);
return tView;
}
Cependant, Swift n'accepte pas de casting d'UIView vers UILabel et par conséquent, je ne peux pas suivre ce chemin qui ressemblerait à ce qui suit:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let label = view as! UILabel
label.font = UIFont(name: "Times New Roman", size: 1.0)
label.text = pickerData[row]
return label
}
Le premier stament (let label ....) lève une exception lors de l'exécution:
EXC-BAD INSTRUCTION (code = EXC_I386_INVOP, sous-code = 0x0)
Un codage Swift plus idiomatique serait:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
guard let label = view as? UILabel else {
preconditionFailure ("Expected a Label")
}
label.font = UIFont(name: "Times New Roman", size: 1.0)
label.text = pickerData[row]
return label
}
Pour Swift 3
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = (view as? UILabel) ?? UILabel()
label.textColor = .black
label.textAlignment = .center
label.font = UIFont(name: "SanFranciscoText-Light", size: 18)
// where data is an Array of String
label.text = pickerData[row]
return label
}
Pour changer le nom et la taille de la police, vous pouvez utiliser viewForRow et une chaîne attribuée:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
var data = pickerData[row]
let title = NSAttributedString(string: data!, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(36.0, weight: UIFontWeightRegular)])
label.attributedText = title
label.textAlignment = .Center
return label
}
Et si vous agrandissez votre taille de police, vous souhaiterez augmenter la hauteur de chaque ligne avec rowHeightForComponent:
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 36.0
}
Si vous voulez que l'étiquette du sélecteur porte sur AUTOSHRINK ...
Définir adjustsFontSizeToFitWidth=true
et minimumScaleFactor=0.5
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel
if let view = view as? UILabel { label = view }
else { label = UILabel() }
label.text = "..."
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 20)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
return label
}
Pour Swift 2.3
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView{
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
label.font = UIFont(name: "Lato-Regular", size: 17)!
label.text = dataArray[row] as? String
label.textAlignment = .Center
return label
}
Pour Swift 3
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView{
print("Returning Custom label")
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
label?.font = UIFont(name: "Lato-Regular", size: 14)!
label?.text = dataArray[row] as? String
label?.textAlignment = .center
return label!
}