1

In my iOS program, I want to set UILabel size in dp's. So that when the iOS device changes, according to scale factor (@1x, @2x, or @3x) my UILabel size will also changes proportionally.

Lets say for eg. @1x scale factor, I have UILabel size = 15pt, then It will automatically become 30pt @2x and 45pt @3x

appleBoy21
  • 632
  • 8
  • 23
  • You can use proportional width constraint with superview , with that you can achieve behaviour which you are looking for – Bhumit Mehta Jul 26 '16 at 05:56
  • @bhumit - can you please tell me in more details. Because I am very beginer to autolayout so that I can try it. Thanks in advance – appleBoy21 Jul 26 '16 at 06:00
  • http://stackoverflow.com/questions/29562015/auto-layout-proportionally-scaling-views/29562197#29562197 You can go through this answer, if you set multiplier 0.3 that will make sure width of label will be 30% of width to view. – Bhumit Mehta Jul 26 '16 at 06:14
  • Thanks bhumit. I just have small doubt. What if I have to change the font size of UILabel also proportional to width of device i.e. [ [ UIScreen mainScreen ] bounds ] . size . width – appleBoy21 Jul 26 '16 at 06:32
  • I am not entirely sure about that but this ans might help . http://stackoverflow.com/questions/4865458/dynamically-changing-font-size-of-uilabel/21382185#21382185 – Bhumit Mehta Jul 26 '16 at 06:43
  • Its ok Bhumit. Thanks for the help. You almost help me out... – appleBoy21 Jul 26 '16 at 06:46

1 Answers1

1

I am not sure whether you can set UILabel size in dp's but instead what you can do is set "Autoshink" to "Minimum Font Size" and provide minimum desire font.

Select your label and assign Autoshrink under Label in Attribute inspector

Rokon
  • 355
  • 3
  • 11
  • Thanks rokon. As per your answer I searched some more things on google. Where I came across these two properties. @property(nonatomic) BOOL adjustsFontSizeToFitWidth @property(nonatomic) BOOL adjustsLetterSpacingToFitWidth These are UILabel properties. I also found minimumFontSize is deprecated in iOS 6. I also found similar kind of article at http://petestabletpage.blogspot.in/2014/10/how-to-make-ios-label-grow-automatically.html What I actually want is depending on width of my device my UILabel font size I want to automatically change. Are these helpful? – appleBoy21 Jul 26 '16 at 08:02
  • Yes, Depending on your device screen font will change but will not go over font size you assign and below minimum font size. – Rokon Jul 26 '16 at 08:38
  • You are most welcome. If my ans helps you to solve your problem please give me a vote. – Rokon Jul 26 '16 at 09:43