xcode 如何使用左侧或右侧移动 UILabel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8796130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move UILabel using left or right side?
提问by wayneh
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.
我是 XCode/iOS 的新手,我试图弄清楚如何基于左侧(或右侧)而不是中心移动标签。
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”是
UILabel
对象的属性吗?例如,在 MS/VB/C#/etc 中。对象具有用于定位的“.left、.right、.top、.bottom” - 在 iOS/Objective-C 中是否有类似的东西?
回答by Noah Witherspoon
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:
center
是的UIView的属性,没有,没有相当的left
,right
或者别的什么东西。您需要手动进行计算:标签的左侧是label.frame.origin.x
,右侧是label.frame.origin.x + label.frame.size.width
。如果要移动标签使其与特定坐标右对齐,则可以执行以下操作:
label.frame = CGRectMake(100 - label.frame.size.width, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
回答by AAV
You need to do the calculation manually to find X and Y coordinate to use in following code.
您需要手动进行计算以找到要在以下代码中使用的 X 和 Y 坐标。
label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y,
label.frame.size.width, labelSize.height);
回答by Jesse Black
You could use center
or frame
to adjust the position of the label. These are properties of UIView
.
您可以使用center
或frame
来调整标签的位置。这些是 的属性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.
frame
返回一个CGRect
. ACGRect
由CGPoint
(原点)和CGSize
(尺寸)组成。原点指定视图的左坐标和上坐标。
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;