Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>UPDATE: Xcode 6 Interface Builder now allows you to select custom fonts and will render them at design time correctly.</h2> <p>I know this question is quite old, but I've been struggling to find an easy way to specify a custom font in Storyboard (or Interface Builder) easily for iOS 5 and I found a quite convenient solution.</p> <p>First, make sure that you've added the font to the project by following <a href="http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app" rel="nofollow noreferrer">this tutorial</a>. Also remember that UINavigationBar, UITabBar and UISegmentedControl custom font can be specified by using the <code>setTitleTextAttributes:</code> method of the <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html" rel="nofollow noreferrer">UIAppearance proxy</a>.</p> <p>Add the categories below to the project for UIButton, UITextField, UILabel and any other component that needs a custom font. The categories simply implement a new property <code>fontName</code> which changes the current font of the element while maintaining the font size.</p> <p>To specify the font in Storyboard, just select the desired element (label, button, textview, etc) and add a <em>User Defined Runtime Attribute</em> with <em>Key Path</em> set to <strong>fontName</strong> of type <em>String</em> and value with the name of your custom font.</p> <p><img src="https://i.stack.imgur.com/SVqdx.png" alt="Custom font Storyboard"></p> <p>And that's it, you don't even need to import the categories. This way you don't need an outlet for every UI component that requires a custom font and you don't need to code it manually.</p> <p>Take into account that the font won't show in Storyboard, but you will see it when running on your device or simulator.</p> <hr> <h2>Category files</h2> <p><strong>UIButton+TCCustomFont.h</strong>:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface UIButton (TCCustomFont) @property (nonatomic, copy) NSString* fontName; @end </code></pre> <p><strong>UIButton+TCCustomFont.m</strong>:</p> <pre><code>#import "UIButton+TCCustomFont.h" @implementation UIButton (TCCustomFont) - (NSString *)fontName { return self.titleLabel.font.fontName; } - (void)setFontName:(NSString *)fontName { self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize]; } @end </code></pre> <p><strong>UILabel+TCCustomFont.h</strong>:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface UILabel (TCCustomFont) @property (nonatomic, copy) NSString* fontName; @end </code></pre> <p><strong>UILabel+TCCustomFont.m</strong>:</p> <pre><code>#import "UILabel+TCCustomFont.h" @implementation UILabel (TCCustomFont) - (NSString *)fontName { return self.font.fontName; } - (void)setFontName:(NSString *)fontName { self.font = [UIFont fontWithName:fontName size:self.font.pointSize]; } @end </code></pre> <p><strong>UITextField+TCCustomFont.h</strong>:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface UITextField (TCCustomFont) @property (nonatomic, copy) NSString* fontName; @end </code></pre> <p><strong>UITextField+TCCustomFont.m</strong>:</p> <pre><code>#import "UITextField+TCCustomFont.h" @implementation UITextField (TCCustomFont) - (NSString *)fontName { return self.font.fontName; } - (void)setFontName:(NSString *)fontName { self.font = [UIFont fontWithName:fontName size:self.font.pointSize]; } @end </code></pre> <p>Also downloadable <a href="https://gist.github.com/redent/7830234" rel="nofollow noreferrer">from GIST</a> and also as <a href="https://gist.github.com/redent/7830234" rel="nofollow noreferrer">a single file</a>.</p> <h2>Troubleshooting</h2> <p>If you run against runtime errors because the <code>fontName</code> property is not specified just add the flag <code>-all_load</code> under <strong>Other linker Flags</strong> in project settings to force the linker to include the categories.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload