TL;DR: Custom fonts couldn't be used programmatically before using them in a Storyboard/xib.
Note: I've checked out this, tried the answers and they didn't work. I've also made sure that they're in target membership.
I've noticed a strange bug while changing segment control title a custom font:
segmentedControl.titleTextAttributes = NSDictionary(objects: [UIFont.init(name: "Comfortaa-Regular",
size: UIFont.systemFontSize)!,
UIColor.white],
forKeys: [NSAttributedStringKey.font as NSCopying,
NSAttributedStringKey.foregroundColor as NSCopying]) as? [AnyHashable : Any]
It couldn't find the font, so unwrapping has failed. But the font could be seen in the Storyboard.
It's properly added to the project, here's the CopyBundle and InfoList:
So here's the catch; if I use it in the Storyboard, it is shown in the font families:
But if not, it's not shown -here I've changed to light and bold has disappeared-, and cannot be used programmatically.
I'm using Xcode Version 9.0 (9A235), and Swift 4. I've also checked OpenRadar and couldn't find a submission regarding this.
The mentioned font: Comfortaa
#EDUsta, I just tried with given font and its work ok, no issue in it, giving the step which i followed:
Add the Comfortaa-Bold.ttf, Comfortaa-Light.ttf, Comfortaa-Regular.ttf Font files to project.
2.Add the entries of Fonts in info.plist file
Make sure the font added in project target.
After it, apply the fonts on UILabel and UISegmentedControl using Swift code given below:
let fontFirst = UIFont(name: "Comfortaa-Bold", size: 16.0)!
segmentFont.setTitleTextAttributes([NSAttributedStringKey.font : fontFirst, NSAttributedStringKey.foregroundColor : UIColor.red],
for: .normal)
labelBold.font = UIFont(name: "Comfortaa-Bold", size: 16.0)
labelLight.font = UIFont(name: "Comfortaa-Light", size: 16.0)
labelRegular.font = UIFont(name: "Comfortaa-Regular", size: 16.0)
its work perfectly, you can check in below screenshot image:
For your reference i am adding the complete project here:
I have the same problem. I can't add exactly the same font family (Comfortaa) programatically - everytime it crashes, but once I add label in launchscreen and set font to Comfotaa-Bold, font loaded from code works fine and doesn't crash. So my solution is to add 3 labels with fonts such as - Comfortaa Bold, Comfortaa Regular, Comfortaa Light in launchscreen and set "hidden" flag on true. This way I'm able to use all of them programatically.
Related
I have few Montserrat font in my app and needed to add italic version of those font, i added them in project, target, info.plist and everywhere it is needed.
When i got to print font names i only see font i had previously
- 0 : "Montserrat-Regular"
- 1 : "Montserrat-Medium"
- 2 : "Montserrat-Light"
- 3 : "Montserrat-SemiBold"
Odd this is when i set italic font in IB to any label then it is showed in code
- 0 : "Montserrat-Regular"
- 1 : "Montserrat-Medium"
- 2 : "Montserrat-Light"
- 3 : "Montserrat-SemiBold"
- 4 : "Montserrat-LightItalic"
- 5 : "Montserrat-Italic"
If i have those fonts in IB UIFont(name: "Montserrat-LightItalic", size: size) will work, but if i remove it from IB it will not find the font.
I use .otf fonts, tried .ttf with same result. I tried to clean project, restart Xcode, deleted app from phone etc. I test on iOS 12 and Xcode is 10.1. Also tried on simulators, iPhone X and iPhone SE(10.2).
Anyone had similar issue or knows why is this happening.
Solved. While adding font in Info.plist file it was added only to one target, not to all. So just tapped on target, went to info and added font there.
Not entirely sure why it is not working. Best potential guess is that maybe you called it something or referenced it, and the name in code is not actually the name. You can do
UIFont.familyNames.forEach({ familyName in
let fontNames = UIFont.fontNames(forFamilyName: familyName)
print(familyName, fontNames)
})
to print out all the fonts and see if it shows up in code. If it is in IB, it should be available programatically. I would run this and see if your fonts appear.
I got a file(please find attached file) which contain font, this font supposed to display icons according to code number.
but i didn't succeed to display any icon with this font. here is my code:
iconTest = UILabel();
iconTest.frame = CGRectMake(0, 0, 100, 100);
onTest.center = self.view.center;
iconTest.font = UIFont(name: "icomoon", size:16.0);
iconTest.text = "e906";
what's the correct way to display icons using font !
download font svg file
You have to set text property like:
iconTest.text = "\u{e906}";
Do not forget to register your custom font in your app.
Follow these steps:
Install fonts by double clicking on them.
Drag and drop fonts in Xcode and make sure they are added to target. (check copy items if needed)
In info.plist, set key Fonts provided by application and add font name for item(under key)
Set font type custom in storyboard for your controls or programatically assign font to UILabel
Code:
//In swift 3
let font = UIFont(name: "iconmoon", size: 15)
btnSignout.setTitle("\u{e913}", for: UIControlState.normal) // set font icon on UIButton
btnSignout.titleLabel!.font = font
// In objectiveC
_lbl.text = [NSString stringWithUTF8String:"\ue92e"]; // set font icon on UILabel
_lbl.font = [UIFont fontWithName:#"iconmoon" size:16];
Note: \u prefix is mandatory, Example for unicode e626, the label text should be \ue626 or \u{e626}
I use icon font in some of my projects. I have create a project to use icon font easily. The README of this project is written in Chinese. But the demo project will show everything clearly. https://github.com/JohnWong/IconFont
Icon font is always used in UILabel by setting text. But I think creating a UIImage from font is more flexible. This is why I create this project.
Method of register font by setting project is in answer provided by martin. Here is a way to register font by code: https://github.com/JohnWong/IconFont/blob/master/IconFont/TBCityIconFont.m#L16
Icomoon.swift: Use your Icomoon fonts with Swift - auto-generates type safe enums for each icon.
Despite it's not IcoMoon, this library is worth noting:
https://github.com/0x73/SDevIconFonts
It integrates FontAwesome, Ionicons, Octicons and Iconic as Icon Fonts.
Assign them with code as easy as:
label.font = UIFont.iconFontOfSize(.FontAwesome, fontSize: 50.0)
label.text = String.fontAwesomeIconWithName(.Twitter)
As noted before, you have to register the font files in your app's Info.plist.
Anyway, regarding IcoMoon: I would use the above library as a starting point. Should be fairly easy (*) to add IcoMoon in a fork (or better as a pull request).
(*) technically spoken, adding a list of convenience shortcuts like .Twitter is quite an expensive task when I have a look at https://github.com/0x73/SDevIconFonts/blob/master/SDevIconFonts/Classes/FontAwesome.swift.
First off, I do realize this has been asked multiple times. Unfortunately the most common answer, isn't working in my case, so please read the full question :)
Here's what I did when I added my custom fonts to the project:
made a folder/group in my project called "Fonts"
added my fonts to that group. (I made sure both the project and "Projetname Tests" was checked as targets)
I checked my project file, -> Build phases -> Copy Bundle resources that the fonts were in there, which they are.
I went to my Plist. and added the group called "Fonts provided by Application", in there I added an item for all my fonts, and changed the strings to the font names
and lastly here's the code that I tried changing the font of a simple label with:
var label = UILabel(frame: CGRectMake(100, 70, 200, 200))
label.text = "BE ON THE"
label.font = UIFont(name: "BabasNeue", size: 106)
self.view.addSubview(label)
The label shows up when I run the app, but neither the size or the font changes.
if anyone wants to check the fontnames are correct, here's the font I'm trying to use: http://www.fontsquirrel.com/fonts/bebas-NEUE
Lastly, I just want to quickly mention that I am fully able to change the font of any labels to my custom fonts using storyboard. But I cannot get it working through code. :/
I think you've got a typo. It has to be called BebasNeue but you named it BabasNeue, with an a instead of an e. It needs to be the exact same name as set in the font-file.
I'm trying to change label font. But every font I set in the Attributes Inspector different from the system font - don't change anything - neither simulator or storyboard. I even tried to set the font programatically with Attributed string - the same System font appears. Thanks for your help.
You cannot use the included iOS fonts in WatchKit at this time. The only one available is System (San Francisco). Source: Apple Developer Forums
You can however use a custom font by adding the font file to the project:
Drag the font files into the project navigator
Include the custom font file in both your WatchKit app and WatchKit
extension bundle.
Add the Fonts provided by application (UIAppFonts) key to both your WatchKit app and your WatchKit Extension Info.plist files
Add this code to awakeWithContext to make sure you know the correct font name to call later in your code:
print("Custom font names:")
print(UIFont.fontNames(forFamilyName: "Exo"))
print(UIFont.fontNames(forFamilyName: "Tabardo"))
Run the app and take note of the font names printed to the debug console. Once you know the correct name, you can add this code somewhere in your WatchKit Extension:
var fontSize = CGFloat(32)
var text = "so cool"
var cstmFont = UIFont(name: "Tabardo", size: fontSize)!
var attrStr = NSAttributedString(string: text, attributes:
[NSFontAttributeName: cstmFont])
firstLabel.setAttributedText(attrStr)
fontSize = CGFloat(36)
text = "right on!"
cstmFont = UIFont(name: "Exo-Regular", size: fontSize)!
attrStr = NSAttributedString(string: text, attributes:
[NSFontAttributeName: cstmFont])
secondLabel.setAttributedText(attrStr)
Enjoy the custom fonts on the watch!
Keep in mind that glances and notifications cannot use custom fonts. If you want to use one there, you'll have to use a rendered image. However because glances and notifications should load quickly you'd want to have that image ready to go when it gets called.
I have a UINavigationBar with a title in the middle. I have added a custom font ("Comic_Andy.ttf") to my app (I have checked info.plist to make sure it's listed, and I have checked the Copy Bundle Resources to make sure it has been added), and I would like the title of the UINavigationBar to be displayed in that font. From what I can gather it seems as though I'm supposed to use this in my ViewController:
myNavigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I placed that method in the viewDidLoad function of the ViewController. I have also tried this in the didFinishLaunchingWithOptions function of the AppDelegate:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I am programming in Swift, in XCode 6 Beta 6. Many resources regarding this task have mentioned using a method called setTitleTextAttributes, which is nowhere to be seen. I can't figure it out for the life of me - I've probably spent close to 3 hours on it by now - I have checked every StackOverflow answer, every website, so please do not mark this as a duplicate. Many thanks in advance!
Instead of using myNavigationBar, try navigationController.navigationBar in the ViewController viewDidLoad function. It worked for me.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
If this doesn't work, try using the .ttf suffix.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy.ttf", size: 22)]
Good luck!
Can also be done for all NavigationBars with UINavigationBar.appearance().
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: Font.mediumFontName, size: 20)!]
Well, it took me the whole day, but I got it. My first problem was I didn't even have a NavigationController, so once I got that all sorted out this is what finally fixed it. Turns out you call the font like this: Comic Andy instead of Comic_Andy. EVEN THOUGH the font is referred to throughout the entire package as Comic_Andy, and the file is named Comic_Andy, you still use Comic Andy.