2017-07-14 190 views
0

如果我设置bottomLabel .text =中文字符,我看不到aboveLabel当我运行它,如果我调试视图层次结构,我可以看到it.so有什么问题?如何添加UILabel上面的UILabel

UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 40)]; 
    bottomLabel.backgroundColor = [UIColor redColor]; 
    bottomLabel.text = @"中文"; 
    [self.view addSubview:bottomLabel]; 
    UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)]; 
    aboveLabel.backgroundColor = [UIColor greenColor]; 
    aboveLabel.text = @"aboveLabel"; 
    [bottomLabel addSubview:aboveLabel]; 

回答

0

您将以上标签添加为bottomLabel的子视图,因此当您将汉字分配给它时,它不会显示。您可以看到它的视图层次结构,因为它被分配了一个框架并添加为子视图。如果你想在UILabel上面添加一个UILabel,你可以添加两个标签作为普通父视图的子视图。 即

[self.view addSubview:bottomLabel]; 
[self.view addSubview:aboveLabel]; 
0

您正在尝试加入aboveLabelbottomLabel内。相反都添加到一个视图或self.view。

1

enter image description here

因为上述标签是像上面图像,这样不显示,如果见上文标记x位置改变该标签然后将它显示底部标签上重叠。

如果x值设置为50,则显示底部标签。

+0

如果我设置bottomLabel.text = @ “ABC” 它工作得很好,我不是很确定你说,你能解释一下吗? – potato

+0

你的上面的标签是底部标签上的Ovelap,所以不显示底部文本。 – Jay

+0

set bottomLabel.text = @“abc”,然后运行,你会看到这两个标签。我的问题是为什么如果我设置bottomLabel.text = @“中文”,我看不到绿色标签? – potato

0
UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 40)]; 
    bottomLabel.backgroundColor = [UIColor redColor]; 
    bottomLabel.text = @"中文"; 
    [self.view addSubview:bottomLabel]; 
    UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bottomLabel.frame)+10, 60, 30)]; 
    aboveLabel.backgroundColor = [UIColor greenColor]; 
    aboveLabel.text = @"aboveLabel"; 
    [self.view addSubview:aboveLabel]; 
3

你为什么不使用NSMutableAttributedString这一个标签。

NSString *text1 = @"Hello"; 
NSString *date1 = @"            12.05 Pm\n"; 

NSString *text2 = @"World"; 
NSString *date2 = @"            11.00 AM"; 


self.lbl.numberOfLines = 0; 

NSString * str = [text1 stringByAppendingString:date1]; 
NSString * str2 = [text2 stringByAppendingString:date2]; 



UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10]; 
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString: str attributes:@{ NSFontAttributeName : text1Font }]; 
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle1 setAlignment: NSTextAlignmentLeft]; 
[paragraphStyle1 setLineSpacing:1]; 
[attributedString1 addAttribute:NSParagraphStyleAttributeName value: paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])]; 


UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10]; 
NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString: str2 attributes:@{NSFontAttributeName : text2Font }]; 
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle2 setLineSpacing:1]; 
[paragraphStyle2 setAlignment: NSTextAlignmentLeft]; 
[attributedString2 addAttribute:NSParagraphStyleAttributeName value: paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])]; 

    [attributedString1 appendAttributedString:attributedString2]; 

    [self.lbl setAttributedText:attributedString1]; 
+0

,因为我试图显示内容的时间,时间应该始终位于内容的最后一行,也许它会重叠内容。 – potato

+0

[效果](https://i.stack.imgur.com/gVHo5.png) – potato

+0

我已更新代码检查其工作 –

0

试试这个代码

的Objective - C

UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)]; 
    bottomLabel.backgroundColor = [UIColor redColor]; 
    bottomLabel.text = @"中文"; 
    [self.view addSubview:bottomLabel]; 
    UILabel *aboveLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 60, 30)]; 
    aboveLabel.backgroundColor = [UIColor greenColor]; 
    aboveLabel.text = @"abc"; 
    [bottomLabel addSubview:aboveLabel]; 
+0

我看不到上述标签。在Xcode 8.3.2 iOS 10.3上同时测试模拟器和iPhone。 – potato

+0

把你的输出截图。 – Jay

+0

[输出](https://i.stack.imgur.com/5mFA6.png) – potato