gpt4 book ai didi

ios - 如何在 UILabel 上面添加 UILabel

转载 作者:行者123 更新时间:2023-11-28 18:49:08 30 4
gpt4 key购买 nike

如果我设置bottomLabel.text = Chinese character,当我运行它时我看不到aboveLabel,如果我调试 View 层次我可以看到它。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];

最佳答案

为什么不使用 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];

关于ios - 如何在 UILabel 上面添加 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45095296/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com