我的目标是让第一行显示居中文本,第二行显示右对齐。
这可能吗?
我使用了NSParagraphStyleAttributeName
传递的属性字符串和所需的对齐方式,但UILabel
似乎只考虑了最后一个段落样式。
NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
// Mathemagika
paragraph.alignment = NSTextAlignmentCenter;
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
initWithString:fullStr];
[attrStr addAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:30.f],
NSParagraphStyleAttributeName : paragraph}
range:NSMakeRange(0, fullStr.length)];
// ©
paragraph.alignment = NSTextAlignmentCenter;
NSAttributedString *attrStr2 = [[NSAttributedString alloc]
initWithString:@"©"
attributes:@{NSBaselineOffsetAttributeName : @20,
NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:10.f],
NSParagraphStyleAttributeName : paragraph}];
[attrStr appendAttributedString:attrStr2];
// Interactive Math Formulae
paragraph.alignment = NSTextAlignmentRight;
NSAttributedString *attrStr3 = [[NSAttributedString alloc]
initWithString:@"\nInteractive Math Formulae\n"
attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:10.f],
NSParagraphStyleAttributeName : paragraph}];
[attrStr appendAttributedString:attrStr3];
UILabel *titleLabel = [UILabel new];
[titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[titleLabel setAttributedText:attrStr];
titleLabel.backgroundColor = self.view.backgroundColor;
[view addSubview:titleLabel];
答案 0 :(得分:1)
我快速浏览了一下,找到了这个链接:
NSMutableAttributedString add different alignments
将标签的使用应用到您的应用程序中,我提出了这个:
NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
paragraph.alignment = NSTextAlignmentCenter;
NSTextTab *t = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:labelFrame.size.width options:nil];
paragraph.tabStops = @[t];
然后在整个过程中定义相同的段落样式,而不是尝试将其从中心更改为右对齐。 (我添加了绿色框,以明确我的边界。)