2017-07-14 12 views
0

bottomLabel .text =中国語の文字を設定すると、aboveLabelが表示されず、ビューの階層をデバッグすると何が問題になるのですか?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

aboveLabelをbottomLabelのサブビューとして追加しています。そのため、中国文字を割り当てると表示されません。フレームが割り当てられ、サブビューとして追加されているので、階層の表示を見ることができます。 UILabelの上に1つのUILabelを追加する場合は、両方のラベルを共通の親ビューのサブビューとして追加できます。 即ち

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

あなたは1つのビューまたはself.viewに両方を追加.Instead bottomLabelの内側aboveLabelを追加しようとしています。

1

enter image description here

ため、上記ラベルは、ラベルのX位置変化前述そのラベルが表示された場合、それが表示されることを表示しないように上記画像のような底ラベルにオーバーラップします。

x値を50に設定すると、ボトムラベルが表示されます。

+0

私はbottomLabel.text = @ "ABC" を設定した場合、それがうまく機能し、私は何を非常にわかりませんあなたはそれをもっと説明できますか? – potato

+0

上記のラベルはボトムラベルのovelapなので、ボトムテキストは表示されません。 – Jay

+0

set bottomLabel.text = @ "abc"、次に実行すると、この2つのlabels.my質問が表示されます。なぜ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

なぜあなたは1 LABELで、このため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

[effect] – potato

+0

私は更新されたコードチェックを行っています –

0

目的にこのコードを試してみてください - 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

関連する問題