如何在UILabel上面添加UILabel

时间:2017-07-14 05:29:13

标签: ios uilabel

如果我设置bottomLabel。text =中文字符,我在运行它时无法看到aboveLabel,如果我调试视图层次结构,我可以看到它。所以'这个问题?

    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];

6 个答案:

答案 0 :(得分:3)

为什么不使用NSMutableAttributedString来使用一个LABEL。

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];

答案 1 :(得分:1)

enter image description here

因为上面的标签与上面的图像重叠,所以如果您看到标签x位置上方的标签发生变化,那么您就不会显示该标签。

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

答案 2 :(得分:0)

您正试图在aboveLabel内添加bottomLabel。而是将两者都添加到一个视图或self.view中。

答案 3 :(得分:0)

您正在将aboveLabel添加为bottomLabel的子视图,因此在为其分配中文字符时不会显示它。您可以看到它的视图层次结构,因为它被分配了一个框架并添加为子视图。如果要在UILabel上方添加一个UILabel,可以将两个标签添加为公共父视图的子视图。 即

<?xml version="1.0" encoding="utf-8" ?> <resources>
 <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar"> 
  <item name="android:windowNoTitle">true</item> 
  <item name="android:windowActionBar">false</item> 
  <item name="android:colorPrimary">@color//blue</item> 
</style>

答案 4 :(得分: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];

答案 5 :(得分: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];