Custom Installed Font Not Displayed Correctly in Uilabel

UILabel with custom font wrongly rendered

Okay, just in case somebody is interested, I figured out a workaround that should work for me. It simply consists of overriding the method drawTextInRect of UILabel, modifying the given rectangle and pass it on to the superclass method.

- (void)drawTextInRect:(CGRect)rect {
CGFloat pointSize = self.font.pointSize;

CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y - (0.25 * pointSize), rect.size.width, rect.size.height);

[super drawTextInRect:newRect];
}

I might have to try out different values other than 0.25 though...

iOS Custom Font Special Characters Wrong On UILabel

I solved this by subclassing UILabel and overriding the -intrinsicContentSize function:

import UIKit

class BiggerLabel: UILabel {
override func intrinsicContentSize() -> CGSize {
var normalSize = super.intrinsicContentSize()
normalSize.height += 4
return normalSize
}
}

Setting custom font for UILabel appearance causes UIDatePicker bug

The only simple work-around I found was to create the UIDatePicker in code (not using storyboards). I got what I wanted.

Attributed string with custom fonts in storyboard does not load correctly

The fix for me was to use an IBDesignable class:

import UIKit

@IBDesignable class TIFAttributedLabel: UILabel {

@IBInspectable var fontSize: CGFloat = 13.0

@IBInspectable var fontFamily: String = "DIN Light"

override func awakeFromNib() {
var attrString = NSMutableAttributedString(attributedString: self.attributedText)
attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length))
self.attributedText = attrString
}
}

Giving you this in the Interface Builder:

Interface Builder custom font with attributed string

You can set up your attributedstring just as you normal do, but you'll have to set your fontsize and fontfamily once again in the new available properties.

As the Interface Builder is working with the custom font by default, this results in a what you see is what you get, which I prefer when building apps.

Note

The reason I'm using this instead of just the plain version is that I'm setting properties on the attributed label like the linespacing, which are not available when using the plain style.

Xcode 8 custom font doesn't show up in interface builder

Try Below Steps: Code tested in Swift 3.

Step 1: Add Your custom font into your project( Make sure Add to Target
ticked).I am using "PermanentMarker.ttf","Pecita.otf" and "AROLY.ttf" font as a test font.

Note: Supporting font Type ttf and otf (Both font types should work)

Step 2: Modify the application-info.plist file.
Add the key "Fonts provided by application" in a new row and add "PermanentMarker.ttf" as new item in the Array "Fonts provided by application".

Your plist should looks like this

Sample Image

Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename

Now, You can access the Custom Font from your viewController. I am testing the font by placing a UIlabel to the Storyboard like below.

Update 2: Working Solution

After, imported your custom font and updated your plist.selectlabel from your storyBoard,goto Attributes Inspectorunder Label>Text type> select to Attributed and choose your custom font from the list.

Sample Image

Sample Image

Sample Image

Output:

Sample Image

Update 1

If your custom font still not listed in Xcode font list.check the related link to your issue

  • http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

  • custom font not displaying on some simulator

Note: Still,You can assign BebasNeue or custom font programatically to your label or button etc. even its not showing in your interface Builder.If you having trouble setting font to your object programatically.try below method.

Assign font to UILabel:

    label?.font = UIFont(name: "BebasNeue", size: 35) // Set to any size

Assign font to UIButton:

    button.titleLabel?.font = UIFont(name: "BebasNeue", size: 35)

Assign font to UITextField:

    textField.font = UIFont(name: "BebasNeue", size: 25) 

Assign font to UINavigationBar:

    navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "BebasNeue", size: 25)!, NSForegroundColorAttributeName: UIColor.red]

Assign font to UIToolBar:

    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "BebasNeue", size: 25.0)!], for: UIControlState.normal)

Output:

Sample Image

Self installed fonts have wrong line height in UILabel

It's probably the fonts. Please try changing only the font used in your app. Use an included iOS font. If the label displays correctly by just changing the fonts used, it is the font's fault. If not, post the code so we can track down the problem.

You can edit fonts with a number of programs such as Fontlab, Fontographer, TypeTool, FontForge, etc. As BobC mentions in a comment, there may be licensing issues.

Set a custom font without using attributed text

With your demo project I have only add

lbl.font = UIFont(name:"Seravek",size:15)

in viewWillAppear and it's showing fine.



Related Topics



Leave a reply



Submit