Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel font size is not correct for bold fonts

I am dynamically create UIlabels and for the first label I use Bold style.

for (int i = 0; i < vehicles.count; ++i) {
    CGRect labelRect = CGRectMake(i * (self.frame.size.width / vehicles.count),
                                  3, self.frame.size.width / vehicles.count,
                                  13);
    UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
    label.textColor = thumbColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = vehicles[i].vehicleClass;
    UIFont *labelFont;
    if (i == 0) {
        labelFont = [UIFont fontWithName:@"Avenir Next-Bold" size:9.0];
    } else {
        labelFont = [UIFont fontWithName:@"Avenir Next" size:9.0];
    }
    label.font = labelFont;

    [vehiclesLabels addObject:label];
    [self addSubview:label];
}

But it draw like this

enter image description here

Why the first label is larger?

like image 717
David Vardanyan Avatar asked Dec 25 '22 06:12

David Vardanyan


2 Answers

For "Avenir Next" family fonts you must use these names:

AvenirNext-MediumItalic, AvenirNext-Bold, AvenirNext-UltraLight, AvenirNext-DemiBold, AvenirNext-HeavyItalic, AvenirNext-Heavy, AvenirNext-Medium, AvenirNext-Italic, AvenirNext-UltraLightItalic, AvenirNext-BoldItalic, AvenirNext-Regular, AvenirNext-DemiBoldItalic

Change Avenir Next-Bold to AvenirNext-Bold, and Avenir Next to AvenirNext-Regular

like image 72
Алексей Абдулин Avatar answered Dec 28 '22 11:12

Алексей Абдулин


Just you need to change the below line of code.

labelFont = [UIFont fontWithName:@"AvenirNextCondensed-Bold" size:9.0];

For all of the font family name checking use following code for that.

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
    NSArray *fontNames;
    NSInteger indFamily, indFont;
    for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
    {
        NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
        fontNames = [[NSArray alloc] initWithArray:
                     [UIFont fontNamesForFamilyName:
                      [familyNames objectAtIndex:indFamily]]];
        for (indFont=0; indFont<[fontNames count]; ++indFont)
        {
            NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
        }

    }
like image 23
Nimit Parekh Avatar answered Dec 28 '22 10:12

Nimit Parekh