简体   繁体   中英

Change UIPickerView font size for different column

I am constructing a UIPickerView, which works fine with my following code.

//Title for Row
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (component==kNumComponent)
        return Number[row];
        else if(component==kSeaComponent)
            return Season[row];
        else
            return Course[row];

}

Where Number, Season and Course are the NSArray storing the string. Here I want to change the font size of course into smaller one. I learned some example here.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
            UILabel *retval = (id)view;
            if (!retval) {
                retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
            }

            retval.font = [UIFont systemFontOfSize:22];
            retval.text = [pickerViewArray objectAtIndex:row];
       return retval;
    }

Is there any quick way for me to combine the above code into my available code to change the font size please? Say, I guess need to pass "Retval" somewhere?

UPDATE: Thanks for Scotts's hint(see below), I have my method works,posted here:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view {

    UILabel *retval = (UILabel*)view;
    if (!retval) {
        retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    }

    retval.font = [UIFont systemFontOfSize:22];

    if (component==kNumComponent)
        retval.text = Number[row];
    else if(component==kSeaComponent)
    {retval.font = [UIFont systemFontOfSize:14];
        retval.text = Season[row];}
    else
    { retval.font = [UIFont systemFontOfSize:14];
        retval.text = Course[row];}

    return retval;
}

To customize your picker's views, you have to use pickerView:viewForRow: . In order to do this, remove pickerView:titleForRow: entirely, and within pickerView:viewForRow: , set retval 's text using the conditional you originally used within pickerView:titleForRow: .

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
 forComponent:(NSInteger)component reusingView:(UIView *)view {

        UILabel *retval = (UILabel*)view;
        if (!retval) {
            retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
        }

        retval.font = [UIFont systemFontOfSize:22];

        if (component==kNumComponent)
            retval.text = Number[row];
        else if(component==kSeaComponent)
            retval.text = Season[row];
        else
            retval.text = Course[row];

        return retval;
}

Or instead of creating a label subview, you can directly edit the title's font by using the UIPickerViewDelegate 's pickerView:attributedTitleForRow:forComponent: method, ex:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title;
    if (component==kNumComponent)
        title = Number[row];
    else if(component==kSeaComponent)
        title = Season[row];
    else
        title = Course[row];

    UIFont *font = [UIFont systemFontOfSize:22];
    NSDictionary *attributes = @{NSFontAttributeName:font};

    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM