13

New to Spritekit and trying to fix the width of a SKLabelNode and then adjust the font of text to fit within the node accordingly.

Have been looking through the docs but cant seem to find anything suitable, like the UILabel function:

A UILabel.adjustsFontSizeToFitWidth = true

Thanks

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • You wont find it :) There is no such a thing on SKLabelNode. What you can do is to calculate everything by yourself. – Whirlwind Dec 25 '16 at 00:33
  • Bummer, sounds painful – dancingbush Dec 25 '16 at 00:36
  • Checkout this : http://stackoverflow.com/a/31545212/3402095 – Whirlwind Dec 25 '16 at 00:39
  • @Whirlwind tx saw that, is that not dependant on if you know the size of the parent node (CGRect), which I don't, I'm building a high scoreview where I'm superimposing a LabelNode on a specific area of a SKSpritenode , to which I don't know dimensions – dancingbush Dec 25 '16 at 00:45
  • So SKSpriteNode is a parent of SKLabelNode ? Also, you say you don't know dimensions of SKSpriteNode ? How is that possible? Just check sprite's frame property...Or I am not following you... – Whirlwind Dec 25 '16 at 00:58
  • Well the SKSpriteNode and label node are children of a Scene, placed superimposed on one another by the scenes coordinate system. The sprite node basically holds a High Scores table static image, while each label node hold a users name which can be entered via user input. If said name is to long it exceeds the dimensions of the spritenode- hope that makes sense – dancingbush Dec 25 '16 at 11:31
  • 1
    Even easier. I think you can use what is given in the link above. But I will try later to write an example for your. – Whirlwind Dec 25 '16 at 11:33
  • Cheers will also have another look, Merry Christmas Day:) – dancingbush Dec 25 '16 at 11:40
  • 2
    Anyone else think it's time for a NSAttributedString version of SKLabelNode? – Confused Dec 26 '16 at 09:11
  • Add a analogous SpriteKit class for UITextField to that – dancingbush Dec 26 '16 at 09:50
  • 1
    Also a performant multiline label node would be nice. @dancingbush Sorry I am not finding time to write an answer yet, but generally I would update label's fontSize based on label's frame while taking sprite's (parent's) frame into account. – Whirlwind Dec 27 '16 at 08:24
  • @Whirlwind tx no problem will come back to it and post a solution if I find one – dancingbush Dec 28 '16 at 00:34

1 Answers1

8

This is a nice function that I found a while back, but I can't remember exactly where

    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)
}

This adjusts the font size of the label to fit the width exactly, but you may want to change the function in order to add some extra padding on all sides.

Aidan Kaiser
  • 501
  • 5
  • 17
  • Many thanks @Aidan , so I pass the SKLabelNode and the parent UITextview dimensions as a CGRect? The plan is get user input via UITextField and input said String within the boundaries a SKLabelNode – dancingbush Jan 04 '17 at 13:58
  • For anyone with little text experience reading this - it only works because there's a single line of text. It's also not guaranteed because font scaling is not linear. You may need to put it in a loop and I'd also include at least a few pixels margin inside the box. – Andy Dent Jul 01 '20 at 09:59