2014-03-02 75 views
0

我目前在单击按钮上运行一个功能。这是我的.m,并且这一切都工作正常。动态添加具有特定文本和坐标的标签

- (IBAction)buttonReact:(id)sender 
{ 
    NSLog(@"clicked"); 
} 

但是,如何让这个函数动态创建一个带有特定文本和特定坐标的标签?

例如,可以在屏幕上“点击”文本并在(10, 10)处协调文本。尽管如此,这些代码总是运行在点击按钮上,所以我希望它能够添加多个标签。意思是,可以在屏幕上显示多个标签,具体取决于按钮被点击的时间/点击的速度。

+0

http://stackoverflow.com/questions/17813121 /以编程方式创建uilabel – sergio

+0

顺便说一句,点击?没有点击它是“挖掘” – doNotCheckMyBlog

回答

1

这里是如何做到这一点:

- (IBAction)buttonReact:(id)sender 
{ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 
                   100.0f, 
                   100.0f, 
                   40.0f)]; 
    label.text = @"Your text"; 
    [self.view addSubview:label]; 
} 

或者你甚至可以去疯狂和随机位置在屏幕上添加标签:

- (IBAction)buttonReact:(id)sender 
{  
    CGPoint randomLocation = CGPointMake(arc4random_uniform(CGRectGetWidth(self.view.bounds)), 
             arc4random_uniform(CGRectGetHeight(self.view.bounds))); 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(randomLocation.x, 
                   randomLocation.y, 
                   100.0f, 
                   40.0f)]; 
    label.text = @"Your text";  
    [self.view addSubview:label]; 
}