Resize a Sklabelnode Font Size to Fit

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.

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

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

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



Leave a reply



Submit