Roei Nadam
Roei Nadam

Reputation: 1780

UIPickerView not change font iOS 9.1

I try to change the font in the title with this code lines. how can I make this work ?

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;

title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];

UIColor *textColor;

textColor = [UIColor whiteColor];

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;

NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor,
                             NSFontAttributeName : font,
                             NSParagraphStyleAttributeName : paragraphStyle};

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

Upvotes: 2

Views: 372

Answers (2)

asafbar
asafbar

Reputation: 931

You can try this:

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
label.text  = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
return label;
}

Upvotes: 2

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

I tried this and it works:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{

    NSString *title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];

    UIColor *textColor = [UIColor whiteColor];

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentCenter;

    NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor,
                                            NSFontAttributeName : font,
                                  NSParagraphStyleAttributeName : paragraphStyle};

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

Upvotes: 1

Related Questions