UIFont returning nil for custom font - ios

I am trying to use the Nunito-ExtraBoldItalic font for my UILabel.
I followed the steps outlined in tutorials and other answers.
Add font to fonts folder. Made sure the target is set to the project.
Add the entry to plist.
Made sure the font is shown in build phase/copy bundle resources. It is shown.
I am using the following code to create a UIlabel:
uiLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 160, height: 50))
let myfont = UIFont(name: "Nunito-ExtraBoldItalic", size: 14)
uiLabel!.font = myfont
uiLabel!.textColor = UIColor(red: CGFloat(51/255.0), green: CGFloat(33/255.0), blue: CGFloat(32/255.0), alpha: CGFloat(100.0))
addSubview(uiLabel!)
But UIFont returns nil.
Postscript name of font is "Nunito-ExtraBoldItalic".
I also tried running the following:
for familyName:String in UIFont.familyNames {
print("Family Name: \(familyName)")
for fontName:String in UIFont.fontNames(forFamilyName: familyName) {
print("--Font Name: \(fontName)")
}
}
My font is not shown.
The font is visible in storyboard and other areas.
I tried using the Nunito-ExtraBoldItalic, Nunito-ExtraBold Italic, Nunito-ExtraBold-Italic as names. None worked.
So I am not sure what the problem is.

Add the font file to the project.
Add "Fonts provided by application" in Info.plist.
<key>UIAppFonts</key>
<array>
<string>CarterOne.ttf</string>
</array>
Make sure the font file is listed in BuildPhase->Copy Bundle Resources.
Make sure the Target Membership is check.
Do a clean build cmd+option+shift+K

You also need to add the font to the .plist file
Please read the following:
https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app
Add the font name to this plist key:
"Fonts provided by application"

Related

Font in iOS11 is incorrectly displayed

I have a font called Poppins-bold (you can find it on Google Font) that in iOS 11 display incorrectly. Here you can see a screenshot with iOS 10:
And screenshot in iOS 11:
Four fonts displayed are:
Poppins Bold with a Storyboard
Helvetica Bold with a Storyboard
Poppins Regular via code
Poppins Bold via code
And my error is with Poppins Bold via code. My code is:
label1.font = UIFont(name: "Poppins-Regular", size: 30)
label2.font = UIFont(name: "Poppins", size: 30)
How is it possible?
I've printed font in the projects and this is my result:
Font Family Name = [Poppins]
Font Names = [["Poppins-Regular", "Poppins"]]
Your printout explains the problem. "Poppins" is a family name. If you have both Poppins Regular and Poppins Bold, the family name defaults to meaning the regular font. This prevents you from accessing the Poppins Bold font by its name, which (unfortunately) is "Poppins".
Instead, use the font descriptor to change Poppins Regular to Poppins Bold:
let font = UIFont(name: "Poppins-Regular", size: 30)!
let desc = font.fontDescriptor
let desc2 = desc.withSymbolicTraits(.traitBold)!
let font2 = UIFont(descriptor: desc2, size: 0)
self.lab.font = font2
I might be a bit late, but in case your question is still relevant, problem can be solved by updating font files from https://fonts.google.com/specimen/Poppins. You will also need to use "Poppins-Bold" font name instead of "Poppins".

Adding custom font to SpriteKit with Swift 3.0

I cannot seem to add my custom font. I have dragged it into my project and added it as a target to my project.
Code creating a label as follows:
// Create Unlock Text
func createUnlockText() {
unlockText = SKLabelNode(fontNamed: "04b_19")
unlockText.name = "unlockText"
unlockText.text = "Goal: \(goalScore)"
unlockText.fontSize = 100
unlockText.fontColor = SKColor(red: 100.0, green: 100.0, blue: 255.0, alpha: 1.0)
unlockText.position = CGPoint(x: 0, y: (self.frame.size.height / 2.8))
unlockText.zPosition = 1
self.addChild(unlockText)
}
I have added the Fonts Provided by application in the plist so I cm confused :(
Here are the steps to add a custom font:
Drag and drop the text font into the project. (You have done it)
Add in info.plist (You have done it)
Check that the source is in the corresponding target. Select the font and in the right menu you have to list the targets, check it
out.
In the project, in the first folder, go to "Build Phases"> Copy Bundles and check that it is added, if not, add it.
Verify that the font name is correct.
To use it is done like this:
You can select it in the storyboard in the viewcontroller.
By code:
label.font = UIFont(name: "Montserrat-Light", size: 12.0)
Then in SpriteKit, are the same steps, but the code it's like this:
SKLabelNode(fontNamed: "Montserrat Light")

custom font becomes a nil value in swift 3

I want to set attributedstring to my UILabel. So I defined the attributes in this way.
let attribute1:[String:Any]=[
NSForegroundColorAttributeName: UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0),
NSFontAttributeName:UIFont.init(name: "Bariol_Bold", size: 15.0)!]
let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)
but I am getting this error in the line:
NSFontAttributeName:UIFont.init(name: "Bariol_Bold", size: 15.0)!]
Does that mean my custom font is not taking? But when I set the font from IB it shows.
fatal error: unexpectedly found nil while unwrapping an Optional value
How can I solve this issue? Please help me.
This commonly happens if actual font name is not same as file name, so the thing is how to find font name.
Take this steps,
Add all font files in your bundle
Add listing of all font files in your info.plist under key : "Font Provided By Application"
Now run the following code,
for fontFamilyName in UIFont.familyNames{
for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
print("Family: \(fontFamilyName) Font: \(fontName)")
}
}
Now see the output in your console, all the available fonts are printed here, check the actual font name of your provided fonts. Now just use the same font name as below:
Now, use that font name for UIFont.init(name: "Actual Font name comes here", size: 15.0)!]
The problem is in the name. Every time it's not necessary that the file name and Font name is similar. To know the exact font name you can use the following function.
func printMyFonts() {
print("--------- Available Font names ----------")
for name in UIFont.familyNames {
print(name)
print(UIFont.fontNames(forFamilyName: name))
}
}
Hope this helps!
Try this
let attribute1:[String:Any] = [NSFontAttributeName: UIFont(name: "Bariol-Bold", size: 18)! ,
NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0)]
let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)

IB custom font atributted text mixing typeface xcode 7.3

I'm using xcode 7.3.1 and trying to use custom fonts on some UILabels in IB.
Already setup custom fonts on my project, however this isn't working as expected.
When I set a custom font on a label with plain text it works, though if I try to use more then one typeface from the same family in a atribbuted text of a UILabel it doesn't work.
This is how it looks like in IB:
This is executing on a real device:
Notice the last UILabel that is defined programatically works, using different typefaces from on an atributtedtext.
Code:
let plainText = "Lorem ipsum dolor sit amet....."
let myMutableString = NSMutableAttributedString(string: plainText, attributes: nil)
myMutableString.addAttributes([NSFontAttributeName: UIFont(name: "SourceSansPro-Light", size: 20)!],
range: NSRange(location: 15, length:42))
myMutableString.addAttributes([NSFontAttributeName: UIFont(name: "SourceSansPro-Bold", size: 20)!],
range: NSRange(location: 67, length:14))
customFontLabel.attributedText = myMutableString
Of course I've already read other questions here that are suggesting to add fonts to info.plist, install fonts on computer and add fonts to project targets(Please don't mark as duplicated, this totally different!!).
Why the fonts aren't shown correctly when cofigured via IB? Is this a bug or Am I doing something wrong?
Source available here: https://github.com/ceduliocezar/swift-labs/tree/master/CustomFontAtributtedTextBug

Change font in UITextInput

I'm trying to develop a keyboard and it should use a different font then the default one. Here's the line:
override func textDidChange(textInput: UITextInput?) {
textInput.font = UIFont(name: "font name", size: 30)
}
But it doesn't work.... How can I fix it?
Thanks.
Follow this steps to use custom Fonts
Drag and drop your font into Xcode
Choose options for adding these files:
Copy items if needed
Create groups
Add to targets:(your app name)
In your Info.plist file add a key named: Fonts provided by application, which is an array
Customize your font:
myUILabel.text = "demoText"
myUILabel.font = UIFont(name: "myCustomFont.ttf", size: 25)

Resources