Resize a SKLabelNode font size to fit?
I was able to solve this thanks to a comment by @InvalidMemory and the answer by @mike663. Basically you scale the label in proportion to the rectangle that contains the label.
func adjustLabelFontSizeToFitRect(labelNode:SKLabelNode, rect:CGRect) {
// Determine the font scaling factor that should let the label text fit in the given rectangle.
let scalingFactor = min(rect.width / labelNode.frame.width, rect.height / labelNode.frame.height)
// Change the fontSize.
labelNode.fontSize *= scalingFactor
// Optionally move the SKLabelNode to the center of the rectangle.
labelNode.position = CGPoint(x: rect.midX, y: rect.midY - labelNode.frame.height / 2.0)
}
Here is the link to the other question.
SKLabelNode custom size between devices
Try replacing
scoreLabel.fontSize = 15
with
scoreLabel.fontSize = UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 30 : 15
How should I resize label nodes in SpriteKit?
Here is my input on your issue.
I would never scale anything UP. Following general guidelines, if you scale down you are generally going to preserve quality. Scaling up creates that pixel art feel.
If you are not using the label node to display numbers that frequently change, use an SKSpriteNode. Use a basic photo editor to type your label and save it as an image that you would assign to a spritenode. Spritenodes are made to handle physics so you should use them to handle all your physics.
If you do need to display numbers of a wide range that change often but you still need them to be affected by physics, you may ask yourself, am I supposed to create an image with each possible outcome that I could assign to my spritenode? The answer is no. You could set you label size to be way bigger than you need and scale it down. To handle physics, you would add this label as a child of some SKSpriteNode that has the physics properties you wish to use for your label. Now, you have a spritenode that interacts with physics as you desire yet also has a label node that displays your information.
Reduce the size of a SpriteKit Node
It seems you're trying to resize a SKLabelNode
. To do so you can change the fontSize
property:
labelNode.fontSize = 10
How to make text the same size on all devices in SpriteKit?
The issue here is that you are trying to scale the fontSize, and this does not really play well with complex decimal numbers. Instead, after you create your label, just scale that to the scale factor that you are using to scale everything else
let text = SKLabelNode(text: "Start")
text.fontSize = 30
text.position = CGPoint(x: 0, y: 0)
text.xScale = xScaleFactor
text.yScale = yScaleFactor
where xScaleFactor
and yScaleFactor
are the factors you are using to determine your scale. (This number should only have to be calculated once, and then stored, if you are not doing that, I would recommend making that change)
Basically in the code you provided it is done like this:
let textRect = CGRect(x: 0, y: 0, width: frame.width * 0.4, height: frame.height * 0.045)
let scaleFactorX = textRect.width / text.frame.width
let scaleFactorY = textRect.height / text.frame.height
Related Topics
What Is the Property Block Declaration Equivalent in Swift of the Following Block Property
Sub-Classing Nstextstorage Causes Significant Memory Issues
How to Connect Localhost (With Invalid Certificate) Using Alamofire
Uitableview - Multiple Selection and Single Selection
How Is a Return Value of Anyobject! Different from Anyobject
Swift 3:Fatal Error: Double Value Cannot Be Converted to Int Because It Is Either Infinite or Nan
Pause an Skaction in Spritekit with Swift
How to Connect Aksequencer to a Akcallbackinstrument
Deploy App with Pre-Populated Core Data
How to Check If 'Any(Not Any)' Is Nil or Not in Swift
How to Integrate Latest Sdwebimage API in My Swift Based Project
How to Read and Write Data to a Text File in Swift Using Playground
Swift Programmatically Set Segues
Can't Use Concrete Subclass to Implement a Property in Protocol in Swift
Nsfetchedresultscontollerdelegate For Collectionview
This Class Is Not Key Value Coding-Compliant for the Key Name.'