38

I was able to change my font color but I also need to change font size, how can I accomplish that? Here's my code for chaning the color,

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

UPDATE: this didn't work:

 NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:40]}];
Scott Zhu
  • 8,341
  • 6
  • 31
  • 38
user3121912
  • 385
  • 1
  • 3
  • 6

8 Answers8

76
- (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;
}
mak
  • 1,173
  • 1
  • 12
  • 18
  • Thank you !! that was helpful. please use `[tView setTextAlignment:NSTextAlignmentLeft]` to align Label text as `UITextAlignmentLeft` is deprecated since iOS 6.0. – Sam Aug 30 '19 at 12:19
19

Here's the Swift version tested on iOS8:

Update in Swift for iOS8, you can add this to your delegate:

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!;
}
bownie
  • 1,608
  • 15
  • 21
8

Updated for 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
}
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
5

You need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate

- (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;
}
the1pawan
  • 1,194
  • 9
  • 26
4

You can use following code to set font of 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;
}
shim
  • 9,289
  • 12
  • 69
  • 108
Maulik Kundaliya
  • 452
  • 3
  • 17
3

Thanks for @Richard Bown

Would this be a better answer for 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
        }
    }
Scott Zhu
  • 8,341
  • 6
  • 31
  • 38
0

I think you have to add NSFontAttributeName in your list of attributes and you could use the class method fontWithName:size: of UIFont

zbMax
  • 2,756
  • 3
  • 21
  • 42
  • I tried, not works: NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:40]}]; – user3121912 Dec 20 '13 at 08:09
  • do you have any ideas? – user3121912 Dec 20 '13 at 08:16
  • 1
    Sorry, my fault. NSFontAttributeName works for a range of text. So you have to add something like `[attString addAttribute:NSFontAttributeName value:/*your font*/ range:NSMakeRange(0, attString.length)];` – zbMax Dec 20 '13 at 08:31
  • replace attString.length by title.length and change type `NSAttributedString` for `NSMutableAttributedString` – zbMax Dec 20 '13 at 09:03
  • 3
    @zbMax I can confirm that this does not work. It ignores all cases of NSFontAttributeName. – Bill Burgess Apr 30 '15 at 16:18
0
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
Mark Bourke
  • 9,806
  • 7
  • 26
  • 30