I'm using custom font in my application. I've used the code below in AppDelegate to change the UINavigationBar appearance.
let navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Pacifico", size: 20)!]
However, the font is getting cut off at the end of the string. The text is "Tell", but you can see that the last character is getting cut off. It works fine if I'm using the default font. Is this a problem with the font itself or it's a bug in Xcode?
Related
I'm trying to change my font from medium to regular, but I keep getting nil when setting the font type for NSFontAttributeName. I can get the font as medium without a problem, but when I try to set it to regular programmatically, my program crashes because the font is nil.
let attrsUnselected = [NSFontAttributeName: UIFont(name: "Roboto-
Regular", size: 15)!, NSForegroundColorAttributeName : UIColor.gray]
let attrsSelected = [NSFontAttributeName: UIFont(name: "Roboto-Medium",
size: 15)!,NSForegroundColorAttributeName : UIColor.black]
Before my app crashes, I can see that the font is actually set to regular.
I don't know how many different ways I get validate that the font has been imported into the project.
Check the following:
The font is added to the target
Its listed under "Copy Bundle Resources" on your target
And does it show up in the list of available fonts when you print them?
When I'm in the storyboard, the navigation bar title is shown as bold in the Storyboard as default but when you run the app in simulator and device, it isn't bold. Annoyingly it isn't possible to change the font size from interface builder.
Below you can see the difference between what's shown on interface builder and the simulator.
Interface builder:
Simulator:
I'm aware that I can change the font using something like this: self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "YOUR-FONT-NAME-WEIGHT", size: 24)!
That would be perfect but I want to keep the font to the default SF font.
Is there a workaround this? Any reason why this happens?
Use this code in your viewDidLoad to change font size
self.navigationController?.navigationBar.titleTextAttributes = [
NSFontAttributeName: UIFont.systemFont(ofSize: 20, weight: UIFontWeightHeavy)
]
For swift 4:
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
]
Before in simulator it looks like
And with that code, it looks like
Simply use UIFont.systemFont to get the default SF font.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize:24, weight: .bold)]
Swift 4:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)]
I know that for changing the font of UINavigationBar he will simply do
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MavenProBold", size: 15)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Tho, i'm trying to change only the font type, and keeping iOS default font size(which i guess he is dynamic among the devices).
Any suggestions?
If you change the font type, you will have to specify a font size. Generally you have to do this manually.
If you want to match the size of another font you see, say in another UILabel, you could do:
CGFloat fontSize = self.label.font.pointSize; // obj-c
let fontSize = CGFloat(label.font.pointSize) // swift
And then assign the size of the font you are setting to be this.
UPDATE
After thorough investigation, the common font size of the UINavigationBar is 34px, which is 17 in your Xcode editor. Remember that you can manually override this view, but that is the default font size.
Scenario:
I'm setting the text font and size for my UITextField.
They also have placeholders.
The code bellow is in my UIViewController:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 18)
let attributesDictionary = [NSForegroundColorAttributeName: DefaultSystemColor.DarkRed]
// Textfileds without borders (and placeholders)
UsernameTextField.borderStyle = UITextBorderStyle.None
UsernameTextField.font = font
UsernameTextField.textColor = DefaultSystemColor.DarkRed
UsernameTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: attributesDictionary)
I'm configuring (in AppDelegate) a global UI setting, that formats all of my UILabels for a certain font size.
The code bellow is in my GlobalUISettings class:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 13)!
var labelAppearace = UILabel.appearance()
labelAppearace.font = font
What's weird in here:
When this UITextField is selected and I'm typing the format is the one I set for the text field (the placeholder are OK too).
But when I leave the field it assumes the behaviour of the UILabel.
I know that because if I comment the UILabel format the textField works properly.
Does anybody have any idea why does it happens?
Try changing the let font to let labelFont because 'font' is global. It might be affecting.
This is correct behavior.
To change the font of the placeholder you have to add the NSFontAttributeName attribute with the desired font as value to the NSAttributedString you assign to attributedPlaceholder.
Example:
let attributesDictionary = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: DefaultSystemColor.DarkRed
]
I am using a non-default font in my apps navigation bar, and this worked absolutely fine prior to Xcode 6.1. Now I get an error on the line of code where I am defining the font type and colour.
This is my code:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
What do I need to change to get this to work again?
UIFont(name:size:) returns an optional, which cannot be used as the value.Change it to this:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
By doing this you unwrap the UIFont? value to NSFontAttributeName. I believe you have to make sure the font is actually there to avoid crash.