Search code examples
objective-cuipickerviewnsattributedstring

Cant change fonts in Picker via NSAttributedString


Here is my code

    self.fontsArray = @[@"Hiragino Kaku Gothic ProN", @"Georgia", @"Times New Roman"];}

-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString* str = self.fontsArray objectAtIndex:row];
    UIFont *font = [UIFont fontWithName:str size:13.0];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                    forKey:NSFontAttributeName];
    NSAttributedString* title = [[NSAttributedString alloc]initWithString:str attributes:attrsDictionary];
    return title;
}

I viewed the solution on this site, but anyway it doesn't work. What is the mistake can it be?


Solution

  • I have found solution.

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* tView = (UILabel*)view;
    NSString* str = [[CSDMainManager sharedManager].fontsArray objectAtIndex:row];
    if (!tView){
        tView = [[UILabel alloc] init];
        UIFont *font = [UIFont fontWithName:str size:20.0];
        tView.font =font;
    }
    tView.text = [NSString stringWithFormat:@"%@", str];
    return tView;
    

    }