如何在UILabel中添加UILabel?

时间:2013-01-09 05:15:55

标签: iphone ios cocoa-touch uilabel

这个问题看起来很平常..但我昨天遇到了问题,我在xib中添加了一个标签并为它创建了插座,我希望在该标签内有多行,按钮,所以我决定添加新的子里面的标签......

UILabel *label;
label = [[UILabel alloc] initWithFrame:initial_rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor whiteColor];
[xib_label addSubview:label]

当我尝试这样做时,它没有工作,然后我在self.view中添加了相同的标签它工作正常..那么当我想在添加的标签中添加标签时我需要做什么使用xib。我在这里遗漏了什么......谢谢......

2 个答案:

答案 0 :(得分:1)

我很久以前就遇到过这种情况。

仅供参考:

我认为问题是initial_rect,它位于视图的框架中,但在xib_label的框架之外。 标签的框架是相对于xib_label的,而不是self.view。

你可以试试这个:

UILabel *label;
CGRect rect = (CGRect){0, 0, 10, 10};
label = [[UILabel alloc] initWithFrame:rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor redColor];
[xib_label addSubview:label];

将backgroundColor更改为红色以清楚地看到标签。

如果您想在标签中显示多行,您可以: label.numberOfLines = 0;

答案 1 :(得分:0)

您还可以尝试使用上述注释将子视图置于前面[xib_lable bringSubViewToFront:label];