Custom font displays in IB but not in simulator

强颜欢笑 提交于 2019-12-23 07:26:14

问题


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?

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.

  • 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.

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.


回答1:


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:




回答2:


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.




回答3:


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.

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.

  • IBInspectable / IBDesignable
  • IBDesignables in Xcode 6 and iOS 8
  • Building custom UI Elements with IBDesignable



回答4:


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


来源:https://stackoverflow.com/questions/31018857/custom-font-displays-in-ib-but-not-in-simulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!