15

是否可以将左右对齐添加到字符串的不同部分?

我试图将对齐属性添加到右侧:

    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrahStyle setAlignment:NSTextAlignmentRight];
    [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:rangeOfDate];

但是整个字符串都向左对齐。

4

2 回答 2

43

您在代码中所做的是设置段落样式(从 \n 到 \n 的文本),并且在同一段落中不可能有多个文本对齐方式。我遇到了同样的问题,最终在 iOS 6 中使用了多个 UILabel :-(

但是,在 iOS 7 中,我注意到 ParagraphStyle 的 TabStops。这实际上使它成为可能,但我发现文档很不充分。但是,我最终让它工作了。您可以设置一个右对齐的 TapStop,这会使您的文本呈现在您指定的停止点的左侧(直到该空间被填满)。为了让我的简单示例正常工作,我必须在第一段之外添加至少一个属性 - 虽然它可能只是背景的清晰 UIColor :-)

以下是 UIView 中 drawRect 的简单示例:

- (void)drawRect:(CGRect)rect
{
    NSString *str = @"to the left\tto the right\nleft again\tright again";
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.maximumLineHeight = 12.0f;
    paragraph.alignment = NSTextAlignmentLeft;

    NSTextTab *t = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight location:rect.size.width options:nil];
    paragraph.tabStops = @[t];

    [att addAttribute:NSBackgroundColorAttributeName value:[UIColor magentaColor] range:NSMakeRange(0, @"to the left".length)];
    [att addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Trebuchet-BoldItalic" size:12.0] range:NSMakeRange(12, 5)];
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(str.length - 4, 2)];
    [att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, str.length)];

    [att drawInRect:rect];
}

该代码将呈现以下内容:

在此处输入图像描述

于 2013-09-20T21:37:08.923 回答
9

以@Verglas 的回答为基础...

您通常在 HTML 中执行此类操作的方式是通过浮动。 像这样的东西::

<div><p style='float: left;'>Left</p><p style='float: right;'>Right</p><div style='clear: both;'></div></div>

如果您可以将其转换为 NSAttributedString 并使其工作,那就太好了:

NSString* html = @"<div><p style='float: left;'>Left</p><p style='float: right;'>Right</p><div style='clear: both;'></div></div>";

NSData* d = [html dataUsingEncoding: NSUTF8StringEncoding];

NSAttributedString* as = [[NSMutableAttributedString alloc] initWithData: d
                                               options: @{
                                                          NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                          NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)
                                                          }
                                    documentAttributes: nil
                                                 error: nil];

可悲的是,它不起作用。

对于第二次尝试,我们可以尝试使用 HTML 表格:

html = @"<table style='width:100%'><tr><td>Left</td><td style='text-align:right;'>Right</td></tr></table>";

奇怪的是,这按预期工作。更令人好奇的是它生成的属性:

2014-08-27 14:27:31.443 testParagraphStyles[2095:60b] range: {0, 5} attributes: {
     NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n    \"<NSTextTableBlock: 0x8d9c920>\"\n), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";

2014-08-27 14:27:31.444 testParagraphStyles[2095:60b] range: {5, 6} attributes: {
    NSParagraphStyle = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n    \"<NSTextTableBlock: 0x8da1550>\"\n), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}

向右滚动并注意对 NSTextTableBlock 的引用。NSTextTable 不是 iOS 上的公共 API,但 NSAttributedString initWithData:options:documentAttributes:error: 使用它从 HTML 生成我们的属性字符串。这很痛苦,因为这意味着我们不能手动构造一个 NSAttributedString(我们必须使用这个 API 从 HTML 生成它)。

从 HTML 构建属性字符串很慢,而且很大程度上没有文档记录。我尽可能避免它。

于 2014-08-27T22:20:35.950 回答