2017-07-14 87 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