I'm using a UILabel as a custom view for my UIPickerView, and I'm trying to pad the label in from the left by 10px or so. However, no matter what frame I set the UILabel to, it gets ignored.
I'm basically trying to make a datepicker, with an "unknown" option in the year component. I'm a bit of a newbie to iOS dev. Is it possible/ would it be more elegant to subclass UIDatePicker and add the "unknown" option?
Here's my code:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* tView = (UILabel*)view;
if (!tView)
{
tView = [[UILabel alloc] initWithFrame:** Any CGRect here **];
tView.backgroundColor = [UIColor redColor];
tView.font = [UIFont boldSystemFontOfSize:16.0];
if (component == 0)
{
tView.textAlignment = NSTextAlignmentCenter;
}
}
// Set the title
NSString *rowTitle;
if (component == 0)
{
rowTitle = [NSString stringWithFormat:@"%d", (row + 1)];
}
else if (component == 1)
{
NSArray *months = [[NSArray alloc] initWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];
rowTitle = (NSString *) [months objectAtIndex:row];
}
else if (component == 2)
{
if (row == 0)
{
rowTitle = @"- Unknown -";
}
else
{
NSDateFormatter *currentYearFormat = [[NSDateFormatter alloc] init];
currentYearFormat.dateFormat = @"YYYY";
NSInteger currentYear = [[currentYearFormat stringFromDate:[NSDate date]] intValue];
rowTitle = [NSString stringWithFormat:@"%d", (currentYear - row)];
}
}
tView.text = rowTitle;
return tView;
}
Thanks!