10

I have set up a UITextView and a UILabel to use a custom font. (It is a vertically mirrored Mongolian font, but I also included English text so that you can see the effect.) The words display in the Interface Builder, but in the simulator most of the characters in the UITextView are just empty boxes. Strangely, in the characters in the UILabel do display correctly in the simulator.

How do I get them to display in the UITextView as well?

enter image description here

Notes:

  • I am using Xcode version 6.3.2. From this SO Q&A I know that custom fonts should work with Xcode 6 in IB.
  • I have added my font file to the project and selected it from the Custom Font List for both the UITextView and the UILabel.
  • I added the key "Fonts provided by application" in my project's Info.plist and added my custom font to it. enter image description here
  • This question is a very similar one (but is different because it doesn't mention the UITextView and UILabel issue). At the time of this writing the OP for that question has not indicated if the top voted answer helped or not. I ran the code in that answer to confirm that my font is really in the bundle. I got the following result, which confirms that it is in the bundle, so that still doesn't solve my problem.

    Family : ChimeeWhiteMirrored
        Font : ChimeeWhiteMirrored
    

Does anyone see my mistake or have any other ideas?

Update

After receiving @darkheartfelt's downvote and comment to my answer below, I recreated my original problem thinking maybe I had missed something. Although in iOS 9 the font displays somewhat differently in the simulator, the basic problem is still there: it looks ok in IB but not in the simulator.

enter image description here

The problem can be reproduced as follows (using Xcode 7.1.1):

  1. Start a new single view project.
  2. Add the the font to the project (can be downloaded here from related github project)
  3. In Info.plist file add "Fonts provided by application" with font name "ChimeeWhiteMirrored.ttf".
  4. Confirm that the target's "Copy Bundle Resources" contains ChimeeWhiteMirrored.ttf.
  5. Add a UITextView to the storyboard.
  6. In the IB Attributes inspector paste in the following text: 1-10:           one two three four five six seven eight nine ten
  7. In the IB Attributes inspector set the font to Custom and the family to ChimeeWhiteMirrored.

This reproduces the problem for the special characters and as I described in my answer below, the only thing I have found to solve this problem it to set the font in code.

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // set custom font
        textView.font = UIFont(name: "ChimeeWhiteMirrored", size: (textView.font?.pointSize)!)
    }
}

If I'm wrong or missing something then please let me know. Until then I'll have to continue using my accepted answer below.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • My solution for that: http://stackoverflow.com/a/40339276/3360896 – Krzysztof Turek Oct 31 '16 at 09:17
  • 1
    This is the **PERFECT WALKTHROUGH** for this problem: http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ – Fattie Jun 16 '17 at 18:08
  • There's no doubt that current iOS/Xcode (2017) is buggy. **Sometimes you have to include the font file name in your plist**, sometimes you do not have to. It's a bug. – Fattie Jun 16 '17 at 18:08
  • @Fattie, Thanks for the link. I am going to be revisiting this again in the future in a framework that I want to make so that will be a useful reference. – Suragch Jun 17 '17 at 01:47

4 Answers4

25

It appears that, while when I first imported the font into my project I selected "Add to target membership", after inspecting the font, it was not added to the target! It worked just fine in Interface Builder, but didn't work in the sim or when compiled.

Selecting the font file in the project folder view and checking the target membership box fixed it:

brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

Create 2 category classes

-------UILabel+ChimeeWhiteMirrored.h--------------

#import <UIKit/UIKit.h>

@interface UILabel (ChimeeWhiteMirrored)
@property (nonatomic, copy) NSString* fontNamelight;
@property (nonatomic, copy) NSString* fontNamebold;
@property (nonatomic, copy) NSString* fontNameboldIltalic;
@property (nonatomic, copy) NSString* fontNameRegular;
@end

--------------------------------------------
-------UILabel+ChimeeWhiteMirrored.m--------------

 #import "UILabel+ChimeeWhiteMirrored.h"

 @implementation UILabel (ChimeeWhiteMirrored)


 - (NSString *)fontNamelight {
     return self.font.fontName;
 }
 - (NSString *)fontNamebold {
     return self.font.fontName;
 }
 - (NSString *)fontNameRegular {
     return self.font.fontName;
 }

 - (NSString *)fontNameboldIltalic {
     return self.font.fontName;
 }



 - (void)setFontNameRegular:(NSString *)fontName {
     self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
 }

 - (void)setFontNamelight:(NSString *)fontName {
     self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
 }

 - (void)setFontNamebold:(NSString *)fontName {
     self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
 }

 - (void)setFontNameboldIltalic:(NSString *)fontName {
     self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
 }




 @end

--------------------------------------------

And add ttf file in project code folder than directly add name of the ttf file to label from properties.

enter image description here

Sandeep Agrawal
  • 425
  • 3
  • 10
  • Do you happen to know the Swift version? If not, no problem, but I'm not very familiar with Objective C so it might take me a little while before I can figure out how to do this in Swift and let you know if it solves the problem. – Suragch Jun 24 '15 at 07:21
  • Can you please create extension. https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html – Sandeep Agrawal Jun 24 '15 at 07:30
  • Thank you for your answer. [I learned a lot](http://stackoverflow.com/a/31026358/3681880) by studying it and although I didn't end up using it exactly, it was the key to finding the answer. +1 – Suragch Jun 27 '15 at 11:15
  • Sorry but these characters aren't appearing in stackoverflow: "         " they just appear as squares. Am I correct in assuming your only problem now is that some of your characters are getting overwritten with emoji? – darkheartfelt Dec 07 '15 at 15:49
1

Common Mistakes With Adding Custom Fonts to Your iOS App is a helpful checklist for getting custom fonts to display correctly. However, none of those things worked in this particular situation.

In addition to the things you did in your notes, you also need to set the font in code. There are a few options for doing that.

Option 1: Hardcode it directly

This is as simple as doing the following:

myUITextView.font = UIFont(name: "ChimeeWhiteMirrored", size: myUITextView.font.pointSize)

Note that the name is the font name, not the font file name (ChimeeWhiteMirrored.ttf).

Option 2: User Defined Runtime Attributes

If you want to avoid doing it in code, you can do it with the User Defined Runtime Attributes. Thank you to @SandeepAgrawal for this idea. Please upvote his answer. The Swift version that worked for me was

import UIKit

extension UITextView {
    
    public var myFontName: String {
        get {
            return self.font.fontName
        }
        set {
            self.font = UIFont(name: newValue, size: self.font.pointSize)
        }
    }
}

Then add the key path, type, and value in the view's identity inspector as is shown in the following image.

enter image description here

Option 3: IBInspectable

From what I have read, using the User Defined Runtime Attributes is the oldfashioned way of doing it. Using IBInspectable (and IBDesignable) allows you to make the changes in the Interface Builder and see the changes right away.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    "you also need to set the font in code." <---this is absolutely NOT true. Add the font to the project, add it as a string item inside the array "Fonts provided by application" in your info.plist, and finally add it to the target's "Copy Bundle Resources". Then it should appear in IB, Storyboards, and most importantly in your app. – darkheartfelt Dec 07 '15 at 00:09
  • 1
    @darkheartfelt, I had done all of those things. I was able to have the custom font appear in IB and the storyboard but not in the app. I redid the whole example again just to test this. You can see my update and response to your comment in the original question above. – Suragch Dec 07 '15 at 08:50
  • "Copy Bundle Resources" is the key – Ashley Mills May 25 '16 at 14:35
  • @darkheartfelt (just slipping off my vive to write this :) ), you know, it's a well-known bug in iOS even today a couple yrs later. sometimes you *do* need to set it in code. sometimes you *do* need to set it in the plist, and sometimes setting it in the plist makes it fail. Just another one of apple's (fortunately fairly rare) "total screw-ups" ! – Fattie Jun 17 '17 at 14:55
  • I had to migrate an older "custom font project" to my new user account. Chris tutorial was a great checklist for it, but sadly I had a different issue. I would like to give you an additional hint on storyboarding with custom fonts together with project migration: 1. verify that your system has the font of choice installed before 2. opening the Xcode project and then compile & run (everything should be fine then) Otherwise the storyboard will wipe out all custom font settings with the system default font! – andreas-supersmart Nov 22 '18 at 09:37
1

If you are using webfont then download.ttf file and drop it into your project . Don't forget to Check mark on copy items if needed on xcode

Next add this on info plist

<key>UIAppFonts</key>
    <array>
        <string>Your fontname.ttf</string>
        <string>Bakersfield Bold.ttf</string>
   </array>

Now take a look the font family name. Which you will find on font file also. From where you have downloaded you will get there also. Like i added font which ttf file name is : Bakersfield Bold.ttf for this fontname is : Bakersfield-Bold Thats it. Like

UIFont *helvFont = [UIFont fontWithName:@"Bakersfield-Bold" size:14.0];
Mehedi Hasan
  • 359
  • 3
  • 10