7

I have a tableview that contains custom cells with a label. I want to change the font of this label to "Roboto-Bold". But it's not working (still the same default font). What I did so far:

  • Downloaded "Roboto-Bold.ttf"
  • Add the font to my project
  • Modified info.plist adding: Fonts provided by application, item0 = Roboto-Bold.ttf
  • Added this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EventOptionCell *cell = (EventOptionCell *)[tableView dequeueReusableCellWithIdentifier:@"EventOptionCell"];

    switch (indexPath.row) {
        case 0:
            cell.optionLabel.text = @"TEST NEW FONT";
            cell.optionLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:20];
            cell.imageView.image = [UIImage imageNamed:@"icon-test.png"];
            break;

        default:
            break;
    }

    return cell;
}
Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
Lücks
  • 3,806
  • 2
  • 40
  • 54
  • Have you checked that the font was also added to your target? – Cornelius Jul 17 '14 at 19:25
  • Sorry, I didn't understand what you mean, can you explain? – Lücks Jul 17 '14 at 19:34
  • 2
    Select the font file in XCodes Project Navigator, then open the file inspector Utilities pane (Cmd-Option-0). You should see a "Target Membership" group there and the target of your project should have the checkbox checked. If it isn't check that and try again. – Cornelius Jul 17 '14 at 21:09
  • Perfect!! Worked :) thanks!!! – Lücks Jul 17 '14 at 22:05
  • Great! I added that as an answer, so that you can accept it if you like – Cornelius Jul 18 '14 at 07:30
  • i am also facing the issue i have checked the target membership they are tick marked and i am able to see them in copy bundle resources . i am using xcode 7 and ios 9 . please suggest whats wrong – Dhanunjay Kumar Apr 18 '16 at 12:45
  • thanks for your Question. I also got a same problem. I didnt add Font in plist. After adding its working fine. – S P Balu Kommuri Sep 01 '16 at 06:21

3 Answers3

8

Sometimes when you add a ttf file to an XCode project it will not be added to your target. This means that it will not be included in the compiled app bundle.

Select the font file in XCodes Project Navigator, then open the file inspector Utilities pane (Cmd-Option-0). You should see a "Target Membership" group there and the target of your project should have the checkbox checked. If it isn't check that and try again.

Cornelius
  • 4,214
  • 3
  • 36
  • 55
  • i am facing the issue even after checking the target membership. kindly suggest. – Dhanunjay Kumar Apr 18 '16 at 12:46
  • 1
    Make sure the font is added correctly to your Info.plist file. Also make sure you are using the correct font name. A good test to see if your font is added correctly to the project is to set a label in Interface Builder to custom font and have a look at the font chooser. – Cornelius Apr 19 '16 at 09:47
  • 1
    Yeah i have rechecked yesterday itself ,its working fine. thanks for reply – Dhanunjay Kumar Apr 19 '16 at 09:55
2

If you did all above and still doesn't work. One more XCODE bug that can cause that - If you never used this custom font on storyboard/xib - add an hidden label with this font(from Interface Builder) and then suddenly it will recognise it programmatically...

-Xcode 7.3.1 - still have this bug

DaNLtR
  • 561
  • 5
  • 21
  • Its so refreshing to see new apple bugs every now and then. Might I ask how did you come upon this solution ? – Abdul91 Apr 28 '17 at 11:55
1

If you added it to plist and project, check if font family names is exactly same as file name, they are sometimes different a little (maybe 'Robot Bold' or so) ...

Snippet which might help (from How to check if a font is available in version of iOS? )

NSArray *fontFamilies = [UIFont familyNames];

for (int i = 0; i < [fontFamilies count]; i++) {
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);
}
Bista
  • 7,869
  • 3
  • 27
  • 55
PetrV
  • 1,368
  • 2
  • 15
  • 30