如何在iOS 7中更改UIPickerView中的文本字体?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7中更改UIPickerView中的文本字体?相关的知识,希望对你有一定的参考价值。

我能够改变我的字体颜色,但我还需要更改字体大小,我该如何实现?这是我的颜色代码,

 - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title = _currencyName[row];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    return attString;
 }

更新:这不起作用:

 NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:40]}];
答案
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* tView = (UILabel*)view;
    if (!tView)
    {
       tView = [[UILabel alloc] init];
       [tView setFont:[UIFont fontWithName:@"Helvetica" size:14]];
       //[tView setTextAlignment:UITextAlignmentLeft];
       tView.numberOfLines=3;
    }
    // Fill the label text here
    tView.text=[wishvalues objectAtIndex:row];
   return tView;
}
另一答案

这是在ios8上测试的Swift版本:

在Swift for iOS8中更新,您可以将其添加到您的委托:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {

    var pickerLabel = view as? UILabel;

    if (pickerLabel == nil)
    {
        pickerLabel = UILabel()

        pickerLabel?.font = UIFont(name: "Montserrat", size: 16)
        pickerLabel?.textAlignment = NSTextAlignment.Center
    }

    pickerLabel?.text = fetchLabelForRowNumber(row)

    return pickerLabel!;
}
另一答案

你需要在picker的委托中实现pickerView:viewForRow:forComponent:reusingView:方法

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* lbl = (UILabel*)view;
    // Customise Font 
    if (lbl == nil) {
          //label size
          CGRect frame = CGRectMake(0.0, 0.0, 70, 30);

          lbl = [[UILabel alloc] initWithFrame:frame];

          [lbl setTextAlignment:UITextAlignmentLeft];

          [lbl setBackgroundColor:[UIColor clearColor]];
           //here you can play with fonts
          [lbl setFont:[UIFont fontWithName:@"Times New Roman" size:14.0]];

   }
      //picker view array is the datasource
   [lbl setText:[pickerViewArray objectAtIndex:row]];


        return lbl;
}
另一答案

针对Swift 4进行了更新:

public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let label = view as? UILabel ?? UILabel()
    label.font = .systemFont(ofSize: 16)
    label.textColor = .white
    label.textAlignment = .center
    label.text = text(for: row, for: component)
    return label
}
另一答案

您可以使用以下代码来设置pickerview的字体。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *tView = (UILabel *)view;
    if (!tView){
        tView = [[UILabel alloc] init];
        [tView setFont:[UIFont .....]];//set font 
            // Setup label properties - frame, font, colors etc
            ...
    }
    // Fill the label text here
    ...
    return tView;
}
另一答案

谢谢@Richard Bown

这对Swift来说会更好吗?

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        if let titleLabel = view as? UILabel {
            titleLabel.text = "Your Text"
            return titleLabel
        } else {
            let titleLabel = UILabel()
            titleLabel.font = UIFont.boldSystemFontOfSize(16)//Font you want here
            titleLabel.textAlignment = NSTextAlignment.Center
            titleLabel.text = "Your Text"
            return titleLabel
        }
    }
另一答案

我想你必须在你的NSFontAttributeName列表中添加attributes,你可以使用fontWithName:size:的类方法UIFont

另一答案
let textLabel = view as? UILabel ?? {
    let label = UILabel()
    label.font = UIFont.boldSystemFontOfSize(16)
    label.textAlignment = .Center
    return label
}()
textLabel.text = Array(componentDataSources[component].keys)[row]
return textLabel

以上是关于如何在iOS 7中更改UIPickerView中的文本字体?的主要内容,如果未能解决你的问题,请参考以下文章

PhoneGap:iOS 7 中的 UIPickerView 不会像在 iOS 6 中那样自动调整字体大小。关于如何在 iOS 7 中实现的任何解决方案?

如何在 iOS 14 中更改 UIPickerView 选择的色调颜色?

如何在 iOS 应用程序中通过 UIPickerView 实现视图之间的转换?

在 iOS 7 中重用 UIPickerView 中的视图

iOS 7 中的自定义 UIPickerView 崩溃

iOS 7 UIPickerView 非选中行曲率修改多个组件