user1828081
user1828081

Reputation: 81

Dynamically Sized UILabel Followed Directly by UIButton

How can I position a UIButton right at the end of a dynamic UILabel?

Here is a screenshot of what I'm trying to achieve:

example

Upvotes: 3

Views: 209

Answers (4)

kernix
kernix

Reputation: 8490

Are you using Autolayout? If so, add horizontal spacing constraint and baseline alignment constraint between the label and the button. Make sure also that numbers of lines of the label is 0.

Upvotes: 1

Kaiusee
Kaiusee

Reputation: 1333

If you want the button to appear at the bottom of the UILabel, use constrains. enter image description here

Upvotes: 0

Clay Bridges
Clay Bridges

Reputation: 11880

I'd override -[UIView layoutSubviews] and do what you want there. Once you do that, the code will look something like Avt's answer. If you want the label to have multiple lines, but a constrained width, you may want to try something like

CGSize labelSize = [yourLabel sizeThatFits:CGSizeMake(someWidth, 9999)];

and use that to set the label's frame.

Upvotes: 0

Avt
Avt

Reputation: 17043

Try

[your_label sizeToFit];
CGRect rect = your_button.frame;
rect.origin.x = CGRectGetMaxX(your_label.frame);
your_button.frame = rect;

Upvotes: 0

Related Questions