1

i'm trying to change a width of UILabel programmatically. I know i can do this by

UILabel.frame.size = CGRectMake(x, y, width, height)

but i don't want to set the x and y value so that it works fine for every phone model. Any suggestion to over come this?

wes. i
  • 627
  • 9
  • 26

3 Answers3

1

It's not required that you add constraints for UILabel' width and height since they have an intrinsic content size, which depends on attributes like text, font, fontsize...

This post, How to set UILabel only width and height and constraints programatically, written for objective-c, should give you an idea on how to tackle the issue.

Community
  • 1
  • 1
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
0

The fact you are changing the frame it means that you are not using auto layout properly.
If you are using auto layout and you want to change a label width I expect you to set a width constraint on the label and change its constant property and then request for a layout update.
If you really applied constraints, remember that they take over programmatically frames, thus your new frame is likely to be set again at its initial value after a layout updates.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

Try:

let newWidth: CGFloat = 100.0 //However wide you want your label to be
myLabel.frame.size.width = newWidth
Benj
  • 736
  • 7
  • 21