0

I have created a label , and now i want to change its postion of display on screen so how can i do that programmatically?

My screen look like this when i first start it.

this is how it is display now

But i want to display it like this when it will first time open.

i want to display it like this

  • Check this link, may be this help you http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel – QueueOverFlow Oct 29 '12 at 08:27

5 Answers5

0

Remove it from view. Then change the frame of the UIlabel . Then add it to view.

[yourLabel removeFromSuperView];
temp.frame=CGRectMake(x,y,width,height);  //set the frame as you want
[self.view addSubView:yourLabel];
zahreelay
  • 1,742
  • 12
  • 18
  • Your example will perfectly work but there is no need to use a temp variable :) Just removing from superview, changing the frame and adding it again would equally work. – Miguel Isla Oct 29 '12 at 08:32
0

You should modify label's frame property. It's of type CGRect:

struct CGRect {
   CGPoint origin;
   CGSize size;
};

To change it's position, change the origin point values. It corresponds to the label's top left point.

CGRect labelFrame = [label frame];
labelFrame.origin.x = 50; // set to whatever you want
labelFrame.origin.y = 100;
[label setFrame:labelFrame]; 
Alexander
  • 8,117
  • 1
  • 35
  • 46
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!");
                         }];


    }
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

//this code used for drag label in view,to click in view label origin change to touch point

    -(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
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

You said you creating label programmatically ,so than you have to set code like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

this should do the work.