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:
How do do something similar, but without using the label center?
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?
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);
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);
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.