Objective c 设置NSMutableAttributeString左对齐和右对齐文本

Objective c 设置NSMutableAttributeString左对齐和右对齐文本,objective-c,text-alignment,nsmutableattributedstring,Objective C,Text Alignment,Nsmutableattributedstring,我正在尝试使用NSMutableAttributedString格式化文本,使其在同一段文本上左对齐和右对齐 我在上查看了类似的答案,但无法使制表位正常工作 我自己的解决方案是执行以下操作 让我的副本+在副本末尾添加物理选项卡(即:\t) 然后为我的聊天时间字符串创建一个右对齐的段落样式,并将其附加到字符串的末尾 我几乎让它工作了 但问题是,时间根本就不对;它似乎被卡在左对齐位置 我希望时间文本在右边 这是我设置时间部分的代码 NSAttributedString *attribute

我正在尝试使用NSMutableAttributedString格式化文本,使其在同一段文本上左对齐和右对齐

我在上查看了类似的答案,但无法使制表位正常工作

我自己的解决方案是执行以下操作

让我的副本+在副本末尾添加物理选项卡(即:
\t

然后为我的聊天时间字符串创建一个右对齐的段落样式,并将其附加到字符串的末尾

我几乎让它工作了

但问题是,时间根本就不对;它似乎被卡在左对齐位置

我希望时间文本在右边

这是我设置时间部分的代码

    NSAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:message.body attributes:attributes];

NSMutableAttributedString *finalizedCopy = [attributedString mutableCopy];
    if (message.sender.remoteID) [finalizedCopy appendAttributedString:tabStops];


    // Add time to the chat bubble
    NSString *messageTime = [[JSQMessagesTimestampFormatter sharedFormatter] timeForDate:message.date];
    NSAttributedString *chatMessageTime = [[NSAttributedString alloc] initWithString:messageTime attributes:attributes];

    NSMutableParagraphStyle *timeParagraphStyle = [[NSMutableParagraphStyle alloc] init];
    timeParagraphStyle.alignment = NSTextAlignmentRight;

    NSMutableAttributedString *dateString = [[NSMutableAttributedString alloc] initWithAttributedString:chatMessageTime];
    [dateString setAttributes:@{ NSFontAttributeName : [UIFont vic_fontOfSize:10],
                                 NSForegroundColorAttributeName: [UIColor vic_darkTextColor]
                                 }
                        range:NSMakeRange(0, dateString.length)];

    [dateString addAttribute:NSParagraphStyleAttributeName value:timeParagraphStyle range:NSMakeRange(0, dateString.length)];

    [finalizedCopy appendAttributedString:dateString];


    attributedString = [finalizedCopy copy];
我想知道怎样才能最好地达到预期的效果

非常感谢

另外,如果我要使用NSTASTOPS,我应该把它们放在哪里,在左手弦的末尾还是右手弦的开头

即:

NSTextTab*tabStop=[[NSTextTab alloc]initWithTextAlignment:nstextAlignment右位置:150个选项:nil];
[段落setTabStops:@[tabStop]];
[finalizedCopy addAttribute:NSParagraphStyleAttributeName值:段落范围:NSMakeRange(0,finalizedCopy.length)]


在我左边字符串的末尾,但我看不到任何结果。

这里有一些通用的Swift代码,我认为它可以满足您的需要。同样的方法也适用于目标C:

    // Paragraph with left and right aligned text

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .left
for tabStop in paragraph.tabStops {
    paragraph.removeTabStop(tabStop)
}
paragraph.addTabStop(NSTextTab(textAlignment: .right, location: 240.0, options: [:]))

let text = NSMutableAttributedString()

let leftAttributes = [
    NSFontAttributeName: UIFont.systemFont(ofSize: 10.0),
    NSForegroundColorAttributeName: UIColor.white,
    NSParagraphStyleAttributeName: paragraph
]

let rightAttributes = [
    NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20.0),
    NSForegroundColorAttributeName: UIColor.white,
    NSParagraphStyleAttributeName: paragraph
]

let data = [
    "Carrots": 23,
    "Pears": 453,
    "Apples ": 2350
]

for (key, value) in data {
    text.append(NSAttributedString(string: key, attributes: leftAttributes))
    text.append(NSAttributedString(string: "\t\(value)\n", attributes: rightAttributes))
}

你试过两个标签和自动布局吗?我知道这不是对你问题的直接回答,只是一个建议。我不能这样做,因为我需要将attributedString传递到JSQMessage播客文件中并返回它<代码>返回[[VICChatMessage alloc]initWithAttributedText:attributedString发件人:发件人ID日期:message.date];}