views:

1467

answers:

2

How can I right align text in a UIPickerView? I tried to make custom UILabels for the row views, but for some reason, nothing is showing up, right-aligned or otherwise. Here's what I wrote:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setText:[NSString stringWithFormat:@"row %d", row]];
    [label setTextAlignment:UITextAlignmentRight];
    return [label autorelease];
}

In case anyone is wondering, I used CGRectZero because I saw it in the UICatalog example.

A: 

Make sure you:

  • Provide an explicit frame that is as high and wide as needed;
  • Set the label's display opaqueness (via .opaque = YES or NO) and background color appropriately (you usually want NO and [UIColor clearColor] respectively).
  • Set the font explicitly.
millenomi
+1  A: 

Your not seeing anything because of CGRectZero. You need to set a size in your case.

In the UICatalog, if your talking about how they used CGRectZero for the CustomView... well if you look at CustomView.m you will see they are actually disregarding the CGRectZero and setting a frame to a size in the initWithFrame:

dizy