简体   繁体   中英

Animated width label with swift

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

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM