您好,在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坐标,以便第二个标签向下移动,但由于某种原因它只会覆盖第一个标签。非常感谢您的帮助。
第2个标签的所有代码都引用第1个变量,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];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句