2

I have two labels. One lane occupies the top left section of the screen and the other occupies the top right section of the screen. Even though it states bottom alignment mode the position is still on the top of the screen

    scoreLabel.text = " Score: 0"
    scoreLabel.fontSize = 70
    scoreLabel.fontColor = SKColor.white
    scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
    scoreLabel.position = CGPoint(x: self.size.width*0.15, y: self.size.height + scoreLabel.frame.size.height)
    scoreLabel.zPosition = 100
    self.addChild(scoreLabel)
    livesLabel.text = "LIVES: 5"
    livesLabel.fontSize = 70
    livesLabel.fontColor = SKColor.white
    livesLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.right
    livesLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.bottom
    livesLabel.position = CGPoint(x: self.size.width*0.85, y: self.size.height + livesLabel.frame.size.height)
    livesLabel.zPosition = 100
    self.addChild(livesLabel)

How can I write code so that both of these labels occupy the the bottom left and right sections of the screen?

  • what's the `self`, you should make clear. you should use frame to set the position of the label. – aircraft Dec 20 '16 at 04:47
  • Hello this link may be help full for you http://stackoverflow.com/questions/27234518/programmatically-align-a-label-from-bottom-of-screen-regardless-of-device-ios – Mitesh jadav Dec 20 '16 at 04:48
  • Easily done using auto layout. Pin both labels to bottom (your can use spacing as needed) and respectively left/right (again using spacing) to their superview. Just remember to turn off the auto resizing flag. –  Dec 20 '16 at 05:33
  • Sorry, you diagram what you're trying to achieve. I can't "see" your objective, at all. Visual thinker... – Confused Dec 20 '16 at 07:18
  • @dfd These are SKLabelNodes, means they are added to a scene and not to the view. You can't apply autolayout constraints to them. – Whirlwind Dec 20 '16 at 07:53
  • 1
    By default a node will be added to the center of the screen because anchorPoint on a sks file is set to 0.5, 0.5 (by default). So when you have your label there, you have to move it by half of screen's height to the bottom. Next if you want it to show in the bottom left corner, then you should set label.position.x = -frame.size.width/2 . After that you should take into account that label's anchorPoint is at 0.5 , 0.5 by default. So you will have to move a label by half of its size to the right. After that you should set horizontal alignment appropriatelly and you will be fine. – Whirlwind Dec 20 '16 at 08:01
  • If you get stuck, let me know and I will make an example for you. By the way, why dont you set those labels visually in sks editor right where you want them ? – Whirlwind Dec 20 '16 at 08:02
  • Just an FYI, alightment mode just means where the font gets rendered inside of the label node, not for the entire scene – Knight0fDragon Dec 20 '16 at 15:33

0 Answers0