iphone - 如何在 UILabel 中添加 UILabel?

标签 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。我在这里错过了什么吗?谢谢……

最佳答案

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

仅供引用:

我认为问题在于 initial_rect,它位于 View 的框架中,但在 xib_label 的框架之外。 label的frame是相对于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];

将背景颜色更改为红色以清晰地看到标签。

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

关于iphone - 如何在 UILabel 中添加 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14228688/

相关文章:

iphone - 如何使用动画切换 View - Objective C

ios - 通过在 objective-c 中进行空气打印,以不同的纸张尺寸打印文档/pdf

ios - 如何在 iOS 的小键盘上添加一个 'Done' 按钮

iphone - MKAnnotationView 图像属性

ios - NSTimezone 从 timezoneId 获取缩写

ios - 在导航 Controller 类的类别中添加左右栏按钮项

iphone - CGAffineTransformMakeRotation不仅可以旋转,还可以移动和拉伸(stretch)UIImageView

ios - NSPredicate 查询不包含特定字符串

ios - UITableView 重新加载数据不起作用

iphone - 如何让 UITextField 响应 UITextFieldDelegate?