ios - 如何在 UILabel 上面添加 UILabel

标签 ios uilabel

如果我设置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/

相关文章:

ios - swift Xcode "EXC_BAD_INSTRUCTION"

html - iOS 从 HTML 安装应用程序

iOS使用socket连接发送数据

ios - trigger.io iOS 应用内购买问题 : "In-app payment failed: Cannot connect to itunes store"

ios - 为 iPhone 6s 编译和较旧的应用程序显示较低的分辨率

iOS7 - 调整多行标签的字体大小以适合其框架

ios - 信奇短信验证SINVerificationErrorDomain Code=3

swift - 将 4 个 UI 标签(与 4 个 slider 链接)值添加在一起

ios - 使用 Storyboard、自动布局和 Swift 的多行 UILabel 和 UITextView

ios - 如何在 Swift 中旋转 UIButton 和 UILabel 的文本?