Creating a SKLabelNode with system font (UIFont to font name) - ios

I am trying to create an Apple Watch SpriteKit app that has a lot of SKLabelNodes inside of it and I am trying to match the default watchOS as closely as possible while using bold, regular, and light weight in my fonts.
The problem is that while I can get a UIFont for what I need I can not however translate that into a SKLabelNode's font.
Here are some attempts. None of them work because the fonts are not found or it does not compile due to types not matching:
SKLabelNode(fontNamed: UIFont.systemFont(ofSize: 99, weight: UIFont.Weight.bold))
SKLabelNode(fontNamed: UIFont.systemFont(ofSize: 99, weight: UIFont.Weight.bold).familyName)
SKLabelNode(fontNamed: UIFont.systemFont(ofSize: 99, weight: UIFont.Weight.bold).description)
SKLabelNode(fontNamed: UIFont.systemFont(ofSize: 99, weight: UIFont.Weight.bold).fontDescriptor)
How can I use system fonts inside of a SpriteKit app? If this is impossible what is a good font that is close enough to the system font for my use?

If you want bold font you can do it this way:
SKLabelNode(fontNamed: UIFont.boldSystemFont(ofSize: 16).fontName)

you need to use the .fontName property and call the font name with the desired weight.
Example:
let exampleOfText = SKLabelNode (text: "here is your text")
exampleOfText.fontSize = 50.0
exampleOfText.fontName = "SFPro-Black" //font weight
note: use the Font Book to help you! :)

Related

What is the system font of iOS13?

What is the iOS 13 system font?
Before iOS 13 I used SFUIDisplay font.
UIFont(name: ".SFUIDisplay-Light", size: UIFont.systemFontSize)
But on iOS 13 it doesn't work.
This bug is so BS. The only way to get around it is by using the systemFont(ofSize:weight:) method directly. AKA, you cannot get the system font using the method UIFont(name:size:), you ll just get Times New Roman for some funny reason. Apple devs must be messing with us. So for the original question above you must use the following:
UIFont(systemFont:UIFont.systemFontSize, weight: .light)
For my situation, I ran into this bug making an extension for UIFont to return the same font in a different size, which must work with custom and system fonts. In order for it to work on xcode 11 ios 13, I had to add a silly check to see if fontName contains ".SF".
extension UIFont {
func forSize(_ pointSize: CGFloat) -> UIFont {
if !fontName.contains(".SF"), let font = UIFont(name: fontName, size: pointSize) {
return font
}
return UIFont.systemFont(ofSize: pointSize, weight: weight)
}
}
If you are aiming to use the system font, you don't really have to worry about its name, you should let the system to do it for you.
let font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
At this point, whenever the system font changes, it will automatically updated.
Moreover
I use a lot of custom fonts. I need to do it
Actually, you could do it without mentioning the font name in case you want to use the system font. For example, you could implement a function that returns the proper font as:
func getFont(name: String = "", size: CGFloat = UIFont.systemFontSize) -> UIFont {
// system font
let defaultFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)
if name.isEmpty {
return defaultFont
}
return UIFont(name: name, size: size) ?? defaultFont
}
For using the system font, call it: getFont(). Otherwise, call it with mentioning the name of the font: getFont(name: ".SFUIDisplay-Light").
However, you might think of doing something like this to get the system font name and then use it:
let systemFontName = UIFont.systemFont(ofSize: UIFont.systemFontSize).fontName
getFont(name: systemFontName)
I'd say it's meaningless since the UIFont.systemFont automatically detects the system font name without the need of mentioning it.
font-family: ".SFCompactText-Regular"; font-weight: normal; font-style: normal
It's "San Francisco (SF) Pro and Compact" font. You can check here https://developer.apple.com/fonts/
Create a font using systemFont(ofSize: CGFloat) -> UIFont.
Then get the fontName and familyName of that font.
Print the above and you have the answer for the current iOS; so run it on iOS 13.

Set Specific Font Weight for UILabel in Swift

When people have asked how to set a bold font, most people suggest:
let boldFont = UIFont.boldSystemFont(ofSize: ___)
But take a look at all the font weights that the standard system font offers:
So my question is how do you set a light, semibold, or heavy font weight? The only way that I know how is:
sectionLabel.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:18];
However, I'm still asking because this isn't strongly typed. Most other class attributes are set by selecting from a fixed set of options and don't require passing a string that I could mistype. I guess I could set my own global enum... But any better ideas?
I couldn't get the UIFontDescriptor to work with the font weight trait but there is another approach.
let font = UIFont.systemFont(ofSize: 20, weight: .light)
Replace .light with whichever value you want from UIFont.Weight which basically matches the dropdown list shown in your question.
You can use this extension. It assigns the weight to the fontDescriptor's weight key and instantiate your font with this new descriptor.
extension UIFont {
func withWeight(_ weight: UIFont.Weight) -> UIFont {
let newDescriptor = fontDescriptor.addingAttributes([.traits: [
UIFontDescriptor.TraitKey.weight: weight]
])
return UIFont(descriptor: newDescriptor, size: pointSize)
}
}
The very old thread, but someone may be interested in how to do it in Swift.
UIFont.Weight defines all of the options:
ultraLight
thin
light
regular
medium
semibold
bold
heavy
black
you can use simply like that, e.g.:
label.font = UIFont.systemFont(ofSize: size, weight: .bold)
or if you want to keep the previous size:
label.font = UIFont.systemFont(ofSize: label.font!.pointSize, weight: .bold)
Even more:
let font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight(500))
If you want to use system fonts, for Swift 5 a simple extension would look like this:
extension UIFont {
func withWeight(_ weight: UIFont.Weight) -> UIFont {
UIFont.systemFont(ofSize: pointSize, weight: weight)
}
}

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".

Swift Changing font programmatically

Hi I am currently setting up views below:
func setupViews() {
self.numberLabel = UILabel(frame: .zero)
self.addSubview(self.numberLabel)
self.numberLabel.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
self.numberLabel.textAlignment = .center
self.numberLabel.textColor = UIColor.white
self.numberLabel.adjustsFontSizeToFitWidth = true
self.numberLabel.minimumScaleFactor = 0.5
}
I would like to change the font of the text inside the label to bold font, however it's difficult to see an easy way to do so, following the syntax principles above.
For Swift 5+ and upto latest version (Swift 5.4) period
Stylizing the Font (SystemFont)
UIFont.systemFont(ofSize: 17, weight: .medium)
where you can set .weight provides various UIFont.Weight properties as given below
.black
.bold
.heavy
.light
.medium
.regular
.semibold
.thin (Looks very cool and I guess Apple also uses this somewhere)
.ultralight
Note that it's only for default SystemFont only.
AppleDocumentation
Changing the Font
UIFont(name: "HelveticaNeue-Thin", size: 16.0)
where name includes the FontName
you can also specify .weight by writing - before weight (If that font supports that weight as Not all fontFamily supports all
types of UIFont.Weight)
This is more preferable method to use for stylising the font if
you're not using default SystemFont
You just set the font property of the label, for example:
numberLabel.font = UIFont.systemFont(ofSize: 10, weight: 200)
I'm amazed there's nothing on SO already on this.
Take a look at the reference docs too: https://developer.apple.com/documentation/uikit/uilabel
By the way, unless you are operating within a closure, or other contexts where the semantics are ambiguous (e.g. in an initialiser) you don't generally need to use self. prefix.
Hope that helps.
If your just using the system font you can also do
UIFont.boldSystemFont(ofSize: 16.0)
Selecting whichever size you need
Here's the documentation on it

How to access the iOS system font programmatically

I am trying to change the font size of the title of a navigation bar. I know I can set its attributes using:
var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]
...
self.navigationController?.navigationBar.titleTextAttributes = attributes
What I cannot seem to find is the correct 'System' font name.
I was after the default, a.k.a System, font name. I tried printing all the available fonts only to discover it does not belong to a family and does not seem to have an explicit name.
I think you need:
NSFontAttributeName : UIFont.systemFontOfSize(19.0)
Or the bold version:
NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)
See this guide for more info on user interface guidelines and fonts.
You can access the system font like this, and even set the weight of the font:
Swift 3, Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)
Swift 2
UIFont.systemFontOfSize(18, weight: UIFontWeightLight)
For the font weight you have the choice between those constants, there available from iOS 8.2:
UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack
SWIFT 4+:
shorter version
UIFont.systemFont(ofSize: 14.0, weight: .regular)
(In line with the answer from Philippe for the latest version)
Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)
Besides all the answers, it's a better idea to use system font with system styles instead of defining custom sizes and weights. To access them programmatically, for example for the headline, you can use this method:
let font = UIFont.preferredFont(forTextStyle: .headline)
I know it is a valid code at least for Swift 5.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]
Just use methods of UIFont (swift):
let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())
Hope it helps!
Try the below code:
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]

Resources