【问题标题】:Dynamically add labels with specific text and coordinates动态添加带有特定文本和坐标的标签
【发布时间】:2014-03-02 20:49:32
【问题描述】:

我目前在单击按钮时运行一个函数。这是我的.m,到目前为止一切正常。

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

但是,如何允许此函数在特定坐标处动态创建具有特定文本的标签?

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

【问题讨论】:

标签: ios button ibaction


【解决方案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];
}

【讨论】:

    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多