想要以编程方式将标签的位置更改为左上角

时间:2012-10-29 08:12:43

标签: iphone ios

我已创建了一个标签,现在我想在屏幕上更改其显示位置,以便我如何以编程方式执行此操作?

首次启动时,我的屏幕看起来像这样。

this is how it is display now

但是我想在第一次打开时显示它。

i want to display it like this

5 个答案:

答案 0 :(得分:0)

从视图中删除它。然后更改UIlabel的框架。然后将其添加到视图中。

[yourLabel removeFromSuperView];
temp.frame=CGRectMake(x,y,width,height);  //set the frame as you want
[self.view addSubView:yourLabel];

答案 1 :(得分:0)

您应该修改标签的frame属性。它的类型为CGRect

struct CGRect {
   CGPoint origin;
   CGSize size;
};

要更改其位置,请更改origin点值。它对应于标签的左上角。

CGRect labelFrame = [label frame];
labelFrame.origin.x = 50; // set to whatever you want
labelFrame.origin.y = 100;
[label setFrame:labelFrame]; 

答案 2 :(得分:0)

   //simply you change origin for label
 //it make animation for moving label in the view
    -(void)your_action
    {
    [UIView animateWithDuration:2
                              delay:1.0
                            options: UIViewAnimationCurveEaseOut
                         animations:^{
                             label.frame=CGRectMake(0, 0, width, height);
                         } 
                         completion:^(BOOL finished){
                             NSLog(@"Done!");
                         }];


    }

答案 3 :(得分:0)

//此代码用于视图中的拖动标签,单击视图标签原点更改为触摸点

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

        UITouch *myTouch = [touches anyObject];
        point = [myTouch locationInView:self.view];

        [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseOut
                         animations:^{
                             label.frame = CGRectMake(point.x, point.y,width, height);
                         }
                         completion:nil];
    }

//enable user interaction for label

答案 4 :(得分:0)

你说你以编程方式创建标签,所以你必须设置这样的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    yourlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    yourlabel.text = @"text";

    yourlabel.userInteractionEnabled = YES;
    [self.view addSubview:alabel];
}

这应该做的工作。