0

I want to change the width of my label since my swift code. If possible, do it in a annimation prograssif for a change. Is it possible ? When I do:

self.lblChoice1.frame.size.width += 50

My exchange label width not ... Why? Thank you in advance.

EDIT : The first response is not working.

print(self.lblChoice1.frame.size.width)
self.lblChoice1.frame.size.width += 150
self.lblChoice1.contentMode = UIViewContentMode.Redraw
self.lblChoice1.setNeedsDisplay()
print(self.lblChoice1.frame.size.width)

This code displays me well in my console:

150.0
300.0

But my label size does not change the display ...

hgcahez
  • 413
  • 6
  • 20
  • Why do you think the code you posted should animate the change? – rmaddy Sep 22 '15 at 20:53
  • No, but it should change the width of my label ... – hgcahez Sep 22 '15 at 20:54
  • @hugo_082 From what I see you should start with some tutorials to learn about this animation first: http://www.raywenderlich.com/76200/basic-uiview-animation-swift-tutorial or you could take a look at this question and answer http://stackoverflow.com/questions/27660540/uiview-animatewithduration-swift-loop-animation – Silviu St Sep 22 '15 at 20:57
  • I've already read the tutorial is, and I understand that. But I understand why the line of code I posted above you does not change the width of my label? – hgcahez Sep 22 '15 at 21:00
  • UI-related tasks should always be done on the main queue, so if you are calling that line above from a different context/queue it won't work – peacer212 Sep 22 '15 at 22:08

2 Answers2

0

You are changing the frame, but you aren't telling the view that it needs to redraw itself. After changing the frame you should then call

self.lblchoice1.setNeedsDisplay()

Although you may need to change the label's content mode to UIViewContentModeRedraw to make sure it redraws.

Although it would be better to use UIView block animation methods to do this.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

I dont think you can edit the UILabel's frame directly. You should change entire frame instead.

var lblChoice1Frame = self.lblChoice1.frame
lblChoice1Frame.size.width += 150

UIView.animateWithDuration(0.3) { () -> Void in
    self.lblChoice1.frame =  lblChoice1Frame
}
Aleš Oskar Kocur
  • 1,381
  • 1
  • 13
  • 29
  • This does not change the initial size of my label. It just creating a progressive display annimation my label ... – hgcahez Sep 23 '15 at 19:36