swift2 在swift中更改拾取器的字体及其大小

zazmityj  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(286)

在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-错误指令(代码=EXC_I386_INVOP,子代码= 0x 0)

czq61nw1

czq61nw11#

适用于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
  }
u1ehiz5o

u1ehiz5o2#

要更改字体名称和大小,可以使用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
}
lrpiutwd

lrpiutwd3#

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

设置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
}
gfttwv5a

gfttwv5a4#

适用于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

    }

适用于雨燕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!

    }
uklbhaso

uklbhaso5#

更符合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
}

相关问题