如果我设置了bottomLabel.text =中文字符,我在运行它时看不到aboveLabel,如果我调试视图层次结构,我可以看到it.so,问题是什么?
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];发布于 2017-07-14 05:37:31
您正在添加aboveLabel作为bottomLabel的子视图,因此当您将汉字分配给它时,它不会显示。您可以看到它的视图层次结构,因为它被分配了一个框架,并添加为子视图。如果要在UILabel之上添加一个UILabel,可以将两个标签添加为公共父视图的子视图。即
[self.view addSubview:bottomLabel];
[self.view addSubview:aboveLabel];发布于 2017-07-14 06:14:20

为什么你不用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];发布于 2017-07-14 05:34:00
您正在尝试将aboveLabel添加到bottomLabel.Instead中,将两者添加到一个视图或self.view中。
https://stackoverflow.com/questions/45095296
复制相似问题