简体   繁体   English

如何使用左侧或右侧移动UILabel?

[英]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. 我是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: 我的问题有两个部分:

  1. How do do something similar, but without using the label center? 如何做类似的事情,但不使用标签中心?

  2. Is ".center" a property of the UILabel object? “ .center”是UILabel对象的属性吗? For example, in MS/VB/C#/etc. 例如,在MS / VB / C#/等中。 objects have ".left, .right, .top, .bottom" for positioning - is there something similar in iOS/Objective-C? 对象有“.left,.right,.top,.bottom”用于定位 - 在iOS / Objective-C中有类似的东西吗?

center is a property of UIView, and no, there's no equivalent left , right , or whatever. center是UIView的属性,不,没有等效的leftright或其他内容。 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 . 您需要手动进行计算:标签的左侧是label.frame.origin.x ,右侧是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. 您需要手动进行计算以找到要在以下代码中使用的X和Y坐标。

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. 您可以使用centerframe来调整标签的位置。 These are properties of UIView . 这些是UIView属性。

frame returns a CGRect . frame返回一个CGRect A CGRect is made up of CGPoint (origin) and CGSize (size). CGRectCGPoint (原点)和CGSize (大小)组成。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM