我的目标是让第一行文本居中,第二行居中,右对齐。
这可能吗?
我使用属性字符串传递 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];
最佳答案
我快速浏览了一下,找到了这个链接:
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];
然后在整个过程中定义相同的段落样式,而不是尝试将其从居中更改为右对齐。 (我添加了绿色框以使其边界位置显而易见。)
关于ios - 在 UILabel 上单独对齐线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568417/