h3dkandi
h3dkandi

Reputation: 1195

Positioning text in UIView

I need to display some text in UIView for which I use label. I am using a xib file. What I need as alignment:

  1. Center label horizontally, vertically in view
  2. maximum width. (Lets say if screen width is 500 I want the label to be 200. But if the screen width is less than 200 then it will be from left view edge margin to right view edge margin.)
  3. Maximum height should be auto. If there is not enough space for text on screen should be screen height minus some fixed space between it and the bottom view edge(if possible width could be expanded too). The space at the bottom is needed for a button under the label.

Is it possible to do this? I don't have enough experience with the interface builder.

Upvotes: 1

Views: 60

Answers (2)

Dhana Gadupooti
Dhana Gadupooti

Reputation: 36

1) apply required autolayout for label and take 3 NSLayoutConstraint IBOutlets attributes, they are 1) Label width 2) label height 3)label y Origin.

2) calculate label width let say Label width Constraint Name is: lblWidthConstraint

  **CGFrame screenFrame = [UIScreen mainScreen].bounds;**

     **if(screenframe.size.width>200){
      self.lblWidthConstraint.constant = 200;
     }else{
       self.lblWidthConstraint.constant = screenframe.size.width-6;//
     }**

3) calculate label Height let say Label height Constraint Name is: lblHeighthConstraint

Based on Label fixed width and Fixed Text find the Label Height, here Max Height should be

CGFloat height = screenframe.size.height-2*(buttonHeight+spacing); self.lblHeighthConstraint.constant = height;

4)calculate label Y Orgin let say Label Y Origin Constraint Name is: lblHeighthYorigin

Based On LabelHeight we should Calculate the Label y origin

yOrign = screenframe.size.height-height/2; self.lblHeighthYorigin.constant = yOrign;

Upvotes: 2

David Ansermot
David Ansermot

Reputation: 6112

Use the alignement contraints for center.
Use the Aspect Ration for the size.
-> Set Aspect ration height = % of the width and a max width of 200px

Use the SizeClass to set another contraint size for screens lesser than 500px. Do it as many time you have screen size different layout.

Upvotes: 1

Related Questions