Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font displays in IB but not in simulator

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.

like image 897
Suragch Avatar asked Jun 24 '15 06:06

Suragch


Video Answer


2 Answers

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:

like image 50
brandonscript Avatar answered Sep 19 '22 11:09

brandonscript


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];
like image 26
Mehedi Hasan Avatar answered Sep 19 '22 11:09

Mehedi Hasan