Angela
Angela

Reputation: 11

UILabel alignment to other UILabels

I have a cell prototype that i'm trying to add two labels to. I have the two labels right next to each other, but the size of the labels are dynamic, so I want the second label to be able to shift depending on the size of the first label.

Basically, I want there to be a fixed gap between the two labels, but the two labels' sizes are dynamic.

How do I do that?

EDIT:

Actually I found out how to do it via Storyboard. If you select the two labels you want to have a fixed gap between, just command select both of them and then go to the corner of storyboard and click the pin menu, which is that little 'H' looking thing in the group of buttons near zoom in/zoom out in the bottom right corner of the storyboard screen.

Upvotes: 1

Views: 345

Answers (2)

Seamus Campbell
Seamus Campbell

Reputation: 17916

UIView -sizeThatFits: and -sizeToFit will allow you to manually calculate a position for the second label. This is slightly more accurate than using the NSString method, as there's more to a UILabel than just the text — this will respect content insets, etc.

Upvotes: 0

Bobo Shone
Bobo Shone

Reputation: 721

Get the label size by this method:

- (CGSize)sizeWithFont:(UIFont *)font

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html.

Then set label's textAlignment to NSTextAlignmentLeft and NSTextAlignmentRight, and set frames by the string size and other offset.

Upvotes: 1

Related Questions