0

In SpriteKit, is there a way to make an SKLabelNode look the same size, regardless of the device, eg: Looks the same size on a iPhone 5 as a 6Plus?

I've tried using this method someone else recommended:

let textRect = CGRect(x: 0, y: 0, width: frame.width * 0.4, height: frame.height * 0.045)
let scalingFactor = min(textRect.width / text.frame.width, textRect.height / text.frame.height)
text.fontSize *= scalingFactor

But it doesn't make all text the same size, as words like "man" aren't as physically tall as words like "High" (due to it's "y" and "h" sticking out).

So is there a method to make text look the same size on all devices? At the moment I create the SKLabelNode like so:

let text = SKLabelNode(text: "Start")
text.fontSize = 30
text.position = CGPoint(x: 0, y: 0)
addChild(text)
  • 1
    what is your constant throughout all your devices? will frame be the same number? Basically I need to know if any scaling/resizing is going on with the scene – Knight0fDragon Dec 21 '15 at 21:09
  • I'd want the font size to be for example 10% of the screen's height, no matter what device it's on. So it should scale with the screen size? –  Dec 21 '15 at 21:11
  • I cant answer that yet, I need to know if your scene is resizing or not – Knight0fDragon Dec 21 '15 at 21:19
  • Yes I believe so? Everything's size is based on % of screen. So for instance, a button's size is "frame.width * 0.5", but nothing changes once you're in the scene. –  Dec 21 '15 at 21:20
  • ok, lets make this simple lol. show me the scaleMode you use, and how you create a game scene – Knight0fDragon Dec 21 '15 at 21:21
  • You mean this? let scene = MenuScene(size: self.size); let skView = self.view as SKView?; scene.scaleMode = .ResizeFill; scene.size = skView!.bounds.size; scene.anchorPoint = CGPoint(x: 0.5, y: 0.5); skView!.presentScene(scene); –  Dec 21 '15 at 21:23
  • ok the scene will resize with the view, thank you, I believe autoconstraints will not affect what you are doing. That is what I was worried about – Knight0fDragon Dec 21 '15 at 21:24
  • Ah okay brill, sorry that took a while to explain –  Dec 21 '15 at 21:25
  • ok now that we have that in place, your label should be a node, so instead of scaling your font size, you should just scale the node based on the screen %. This can be done with xScale and yScale, so you dont even have to worry about finding the min between the two – Knight0fDragon Dec 21 '15 at 21:27
  • if you can paste how you create your label, I will create the answer how you should be scaling your label – Knight0fDragon Dec 21 '15 at 21:36
  • Hang on I'll add it to my question... –  Dec 21 '15 at 21:38
  • Done, I've added it to the question. –  Dec 21 '15 at 21:43

2 Answers2

2

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
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
1

I think it's more like an algorithm question. Think about you need to implement the same thing in TV, iPad or in the iPhone device. You should think about storing its absolute value rather than its actual value.

The formula should be width for store value = actual width for this device / device width. The same with the height. Then, if you use the same image data in other devices. You will just need to multiply the new device width/height.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • I don't think it's that complicated, I simply want to be able to create SKLabelNodes, and have them look the same size on all iPhones? –  Dec 21 '15 at 21:48
  • It's not complicated. it's just how you store your width and height. – Lucas Huang Dec 21 '15 at 22:21