快速更改选择器的字体及其大小

Posted

技术标签:

【中文标题】快速更改选择器的字体及其大小【英文标题】:changing font and its size of a picker in swift 【发布时间】:2015-11-11 16:22:15 【问题描述】:

使用 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

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

EXC 错误指令(代码=EXC_I386_INVOP,子代码=0x0)

【问题讨论】:

这个链接[link]***.com/questions/27455345/…解决了问题。 【参考方案1】:

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

【讨论】:

【参考方案2】:

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

【讨论】:

您也可以将前几行替换为:let label = (view as?UILabel) ?? UILabel()【参考方案3】:

如果您希望选择器标签为 AUTOSHRINK...

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

【讨论】:

【参考方案4】:

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

    

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

    

【讨论】:

【参考方案5】:

更惯用的 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

【讨论】:

对于像我这样在尝试这种方法时遇到错误并花了太多时间试图找出问题所在的人,这里有一种可行的替代方法:***.com/questions/27455345/…跨度>

以上是关于快速更改选择器的字体及其大小的主要内容,如果未能解决你的问题,请参考以下文章

如何在 android 中设置 react native expo 选择器的样式

CSS-属性&&选择器

如何更改解决方案资源管理器的字体大小(在 Visual Studio 中)

如何更改 UIPickerView 中的字体大小? [复制]

Swiftui - ***选择器,更改对齐方式和字体大小

如何更改 WPF 日期选择器的图标?