更改uilabel的文本然后添加新的uilabel

时间:2013-02-01 00:14:00

标签: ios xcode uilabel uigesturerecognizer

我想更改标签的文本,然后让用户将其移动到屏幕上的所需位置(当前正在工作)(用户点击 - “添加文字”)。

一旦将它们放置在他们想要的位置。我想要“添加文本”按钮来创建用户可以移动的新标签。我不确定如何动态创建这些以确保手势识别器与新标签一起工作。谢谢你的建议。

这就是我现在所拥有的,但还不行。


-(IBAction)addText:(id)sender
{
    textView.hidden=YES;


    labelShirt.text= textField.text;
    [textField resignFirstResponder];

    [self addTextButtonPressed];

}



-(void)addTextButtonPressed
{
// CGRect *textFrame =
    // myInitialFrame is a CGRect you choose to place your label
    UILabel *myNewLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,100,100)];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(labelMoved:)];
   myNewLabel.text =textField.text;

[self.view addSubview:myNewLabel];
}

-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.view];
    sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}

1 个答案:

答案 0 :(得分:1)

// The action that is added to your add text button
-(void)addTextButtonPressed
{
    // myInitialFrame is a CGRect you choose to place your label
    UILabel *myNewLabel = [[UILabel alloc] initWithFrame:myInitialFrame];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(labelMoved:)];
    myNewLabel.text = @"My initial text";
    // EDIT
    [self.view addSubview:myNewLabel];
    [myNewLabel addGestureRecognizer:panGestureRecognizer];
}

-(void)labelMoved:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:self.view];
    sender.view.frame = CGRectOffset(sender.view.frame, translation.x, translation.y);
}

我不知道这是否足以解决您的问题,如果您仍需要更多解释,请发表评论。