在 swift 中更改选择器的字体及其大小

changing font and its size of a picker in swift

对于 Objective-C,我将下面显示的代码用于 set/change 选择器的字体系列和大小:

- (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;
}

但是,Swift 不接受从 UIView 到 UILabel 的转换,因此我无法遵循如下所示的路径:

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
}

第一个语句 (let label ....) 在 运行 时间抛出异常:

EXC-BAD INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

更惯用的 Swift 编码是:

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
}

要更改字体名称和大小,您可以使用 viewForRow 和属性字符串:

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

}

如果您使字体变大,则需要使用 rowHeightForComponent:

增加每行的高度
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 36.0
}

对于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
  }

为swift2.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

    }

为Swift3

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!

    }

如果您希望选择器标签自动收缩...

设置adjustsFontSizeToFitWidth=trueminimumScaleFactor=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
}