-3

im trying to use 3 labels in view. And fill this labels random and daily. Label texts random, so text size flexible. In Android its doing this auto but in swift it isn't resizing labels to text. There are some pictures:

Android:

enter image description here

iOS:

enter image description here

How can i auto resize these labels to text?

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Ahmet Özrahat
  • 325
  • 2
  • 7
  • 18
  • Pls explain your line `auto resize these labels to text?` either you wanted to change labels to text or you wanted to change size of the labels as per their content.???? – dahiya_boy Feb 02 '17 at 10:27
  • I'm putting these labels to random texts, texts are big to label so its writing end of the label ... I think first two label doesnt have enough space to write. But third one has. Sorry, I can just explain like that – Ahmet Özrahat Feb 02 '17 at 10:32
  • 1
    Still not getting what you want? For more info, in `iOS` text and labels are two different things. So which one you are talking about. Its better if you can show me your code. – dahiya_boy Feb 02 '17 at 10:35
  • go with this [Answer](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights?rq=1) – dahiya_boy Feb 02 '17 at 10:39
  • or [this answer](http://stackoverflow.com/questions/27214283/resizing-the-height-of-uilabel-to-fit-text). – davidethell Feb 02 '17 at 10:42
  • Using auto-layout this is automatic. What have you tried? Can you post your code? – redent84 Feb 02 '17 at 11:16
  • @AhmetÖzrahat I don't know what are you trying, but you may try this- `yourLabel.numberOfLines = 0` **And** `yourLabel.sizeToFit()` – iDeveloper Feb 02 '17 at 11:21

2 Answers2

0

set the constraint of label, height constraint should be greater than. and set number of line zero of label

enter image description here

Set Constraint

set constraint like this

IOS Singh
  • 617
  • 6
  • 15
  • Can you provide an sample about how to solve this problem with only label height constraint and number of line? – Nhat Dinh Feb 02 '17 at 11:20
0

Try to achieve it using autolayout constraints instead of calculating height according to text size.

Add proper constraint on UI controls so it will automatically calculate their height and width.

Note -

  1. Just make sure do not fix the height of UI controls using height constraint.
  2. And set these two properties of UILable from Attributes inspector section of storyboard:

    • lines to 0

    • Line break mode to word wrap

Prema Janoti
  • 836
  • 5
  • 18
  • Tell me the reason to not fix height of an UILabel. numberOfLines and line break mode is needed but you will not able to fit your label without a specified height. – Nhat Dinh Feb 02 '17 at 11:18
  • 1
    If you will fix the height of UILable, it will not automatically calculate their height. – Prema Janoti Feb 02 '17 at 11:22
  • When you use autolayout, your view height will not be effected if you set your view height by code like: yourview.height = 100, its only be effected if you set constrain by code whith higher priority – Nhat Dinh Feb 02 '17 at 11:30
  • 1
    iOS use intrinsic content size to calculate the minimum space required by a UI element to be layout. You don't need to manually set the height of a `UILabel` and, if the view is well constructed, neither their parent's – redent84 Feb 02 '17 at 11:35