wayneh
wayneh

Reputation: 4413

How to move UILabel using left or right side?

I'm new to XCode/iOS and I'm trying to figure out how to move a Label based on the left (or right) side, not the center.

All I can find is something like this:

[myLabel setCenter:CGPointMake(x,y)];

I've also seen this variant:

myLabel.center=CGPointMake(x,y);

My question has two parts:

  1. How do do something similar, but without using the label center?

  2. Is ".center" a property of the UILabel object? For example, in MS/VB/C#/etc. objects have ".left, .right, .top, .bottom" for positioning - is there something similar in iOS/Objective-C?

Upvotes: 1

Views: 11465

Answers (3)

Jesse Black
Jesse Black

Reputation: 7976

You could use center or frame to adjust the position of the label. These are properties of UIView.

frame returns a CGRect. A CGRect is made up of CGPoint (origin) and CGSize (size). The origin specifies the left and top coordinates for the view.

CGRect frame = label.frame;
label.origin.x = desiredLeft;
label.frame = frame;

or

CGRect frame = label.frame;
label.origin.x = desiredRight-label.size.width;
label.frame = frame;

Upvotes: 0

AAV
AAV

Reputation: 3803

You need to do the calculation manually to find X and Y coordinate to use in following code.

label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y, 
label.frame.size.width, labelSize.height);

Upvotes: 0

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

center is a property of UIView, and no, there’s no equivalent left, right, or whatever. You need to do the calculation manually: the label’s left side is label.frame.origin.x and its right side is label.frame.origin.x + label.frame.size.width. If you want to move the label so it’s right-aligned with a particular coordinate, then you can do something like this:

label.frame = CGRectMake(100 - label.frame.size.width, label.frame.origin.y, label.frame.size.width, label.frame.size.height);

Upvotes: 3

Related Questions