ios - 在 UILabel 上单独对齐线条

标签 ios objective-c ios7 uilabel uitextview

我的目标是让第一行文本居中,第二行居中,右对齐。

这可能吗?

我使用属性字符串传递 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];

然后在整个过程中定义相同的段落样式,而不是尝试将其从居中更改为右对齐。 (我添加了绿色框以使其边界位置显而易见。)

enter image description here

关于ios - 在 UILabel 上单独对齐线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568417/

相关文章:

iphone - UIImagePickerController 有时不会初始化相机 View

ios - 了解 iOS Cookie 接受政策

objective-c - 从带有附件和主题的外部应用程序打开 Mail.app

ios - 如何在基于部分的 TableView 中获取单元格在切换更改事件时的索引路径

ios7 - IOS : Application tried to present a nil modal view controller on target

xcode - 仅在播放视频时使用横向模式。代码。 IOS 7

ios - 如何在 iOS 项目中构建和使用 C 库?

ios - 如何在 SpriteKit 中为 physicsWorld 设置 contactDelegate?

ios - 为什么我的 SpriteNode 没有被添加到场景中?

ios - 在ios后台重复运行代码