gpt4 book ai didi

ios - 如何在 Xcode 中以编程方式添加多个 UILabel?

转载 作者:行者123 更新时间:2023-11-29 10:40:36 25 4
gpt4 key购买 nike

Hello in Xcode 我正在尝试以编程方式向我的图像添加两个标签,但是当我添加第二个标签时,它将覆盖我的第一个标签。这是我的代码:

    //first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:@" @%@",[data objectForKey:@"username"]];
[self addSubview: caption];

//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:@" Testing"];
[self addSubview: bottomBox];

我尝试将“0”更改为 -200 以修改 Y 坐标,以便第二个标签向下移动,但由于某种原因它只是覆盖了第一个标签。非常感谢任何帮助。

最佳答案

第二个标签的所有代码都引用了第一个变量 caption 而不是 bottomBox。你想要:

//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = @"Testing";
[self addSubview: bottomBox];

关于ios - 如何在 Xcode 中以编程方式添加多个 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com