简体   繁体   中英

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

在此输入图像描述

Why the first label is larger?

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

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]);
        }

    }

Remove the spaces from the font name it will works for you

UIFont *labelFont;
    if (i == 0) {
        labelFont = [UIFont fontWithName:@"AvenirNext-Bold" size:9.0];
    } else {
        labelFont = [UIFont fontWithName:@"AvenirNext-Regular" size:9.0];
    }
    label.font = labelFont;

In your current code the font is not applied to first string in that array. If you try to print the log for labelfont it will be nil for i=0. And if you print the log for label.font you could see the following output

(lldb) po label.font
<UICTFont: 0x7fbbb36185a0> font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 17.00pt

For the bold font to be working please try the following,

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

Hope this will help.

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