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.
But i want to display it like this when it will first time open.
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.
But i want to display it like this when it will first time open.
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];
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];
//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!");
}];
}
//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
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.