angul
angul

Reputation: 55

How do I change UILabel size depending on its font size?

@IBAction func sizeChanged(sender: UISlider) {
let senderValue = CGFloat(sender.value)
myLabel?.font = UIFont(name: (myLabel?.font.fontName)!, size:senderValue * 20)}

I want to change myLabel.font size with a slider, but myLabel does not change adjust its width and height as the font size increases.

How do I change the UILabel size to follow its font size?

Thanks.

Upvotes: 0

Views: 136

Answers (2)

Sujith Chandran
Sujith Chandran

Reputation: 2721

myLabel?.sizeToFit() will set the size of label to fit its content...just make sure you haven't added height and width constraints for the label.

Upvotes: 0

ndmeiri
ndmeiri

Reputation: 5029

After updating the font size of the UILabel, you'll want to call sizeToFit().

myLabel?.sizeToFit()

According to the UIView Class Reference,

Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.

Upvotes: 1

Related Questions