web-dev-qa-db-ja.com

swiftでピッカーのフォントとそのサイズを変更する

Objective-Cでは、以下に示すコードを使用して、フォントファミリーとピッカーのサイズを設定/変更しました。

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

最初の文(ラベルは....)は実行時に例外をスローします。

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

9
Robert Brusa

より慣用的な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
}
1
GoZoner

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
  }
23
Wilson

フォント名とサイズを変更するには、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
}
6
John Pavley

ピッカーラベルをAUTOSHRINKにしたい場合...

セットする adjustsFontSizeToFitWidth=trueおよび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
}
5
Derek Soike

Swift 2.の場合

 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

    }

For Swiftの場合

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!

    }
2
Nischal Hada