Anand Prakash
Anand Prakash

Reputation: 311

dynamic positioning of button according label text in ios

I have taken a label and two button below the label. I want to shift buttons upward and downward dynamically according to text in label. As in the image I have a label and three line data within it.and two Button below it

in this image I have data in single line in label then button should be shift upward accordingly

Upvotes: 1

Views: 818

Answers (3)

MobileGeek
MobileGeek

Reputation: 2512

Use with :

For Accept Button:

btnAccept!.frame = CGRectMake(CGRectGetMinX(txtView!.frame), CGRectGetMaxY(txtView!.frame) + 20, btnAccept!.frame.size.width, btnAccept!.frame.size.height)

For Decline Button:

btnDecline!.frame = CGRectMake(CGRectGetMinX(txtView!.frame), CGRectGetMaxY(txtView!.frame) + 20, btnAccept!.frame.size.width, btnAccept!.frame.size.height)

variable btnAccept is accept button

variable btnDecline is decline button

variable txtView is textview

Upvotes: 0

Sujit
Sujit

Reputation: 658

Instead of hardcoding, use Autolayout.

Set Accept button left edge constraint to label left edge constraint.
Set Decline button right edge constraint to label right edge constraint. 
Add vertical space between buttons and label.

Take label height constraint reference and set it's constant at runtime which is equal to label height.

Upvotes: 0

iPhone
iPhone

Reputation: 4170

CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

According to label's height set your button's frame

Upvotes: 1

Related Questions