3

我正在尝试格式化一些文本NSTextView以放置在黑色背景的单行中。一段文本需要左对齐,另一段(但仍在同一行)需要居中。

NSString *header = [NSString stringWithFormat:
        @""
            "<table style=\"width: 100%; background: black; "
            "color: white; font-family: Arial; "
            "font-size: 16px;\"><tr><td>"
                "1. zadatak"
            "</td><td align=\"center\" width=\"100%\">"
            ""
                "%@"
            ""
            "</td></tr></table>"
        "", [self.problemname.stringValue uppercaseString]];

不幸的是,无论我指定什么宽度,NSTextView似乎都忽略了表格宽度。

任何解决方法、不同的方法或其他建议?


有关问题背景的更多信息。

我编写了一个应用程序,用于编写编程竞赛的任务。每个任务作者都以不同的格式提交内容,因此让他们将其标准化为一个简单的捆绑包将大有裨益。

我需要这个作为打印模块。我正在采取几个NSTextViews 并将它们缝合在一起。

这些竞赛具有我们应该遵循的以下文档格式 -示例 PDF ( GoogleDocs ):

Contest header
+------------------------------------------------+
| 1st Task           TITLE             20 points |
+------------------------------------------------+

Introductory text here.


                    Input

Explanation of input data


                   Output

Explanation of output data


                   Sample Data

Input:       | Input:
abcd         | bcde
             |
Output:      | Output:
efgh         | fghi
             |
More info:   |
info text    |
4

2 回答 2

3

修改Apple 以编程方式将表添加到 NSAttributedString 的示例代码

- (NSMutableAttributedString *) printTitleTableAttributedString
{
    NSMutableAttributedString *tableString = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
    NSTextTable *table = [[NSTextTable alloc] init];
    [table setNumberOfColumns:3];

    [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"1. zadatak\n"
                                                                                      table:table
                                                                              textAlignment:NSLeftTextAlignment
                                                                                        row:0
                                                                                     column:0]];

    [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:[self.problemname.stringValue stringByAppendingString:@"\n"]
                                                                                      table:table
                                                                              textAlignment:NSCenterTextAlignment
                                                                                        row:0
                                                                                     column:1]];

    [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"20 bodova\n"
                                                                                      table:table
                                                                              textAlignment:NSRightTextAlignment
                                                                                        row:0
                                                                                     column:2]];
    [table release];
    return [tableString autorelease];
}



- (NSMutableAttributedString *) printTitleTableCellAttributedStringWithString:(NSString *)string
                                                                        table:(NSTextTable *)table
                                                                textAlignment:(NSTextAlignment)textAlignment
                                                                          row:(int)row
                                                                       column:(int)column
{
    NSColor *backgroundColor = [NSColor colorWithCalibratedRed:0 
                                                         green:0 
                                                          blue:0x80/255. 
                                                         alpha:0xFF];;
    NSColor *borderColor = [NSColor whiteColor];


    NSTextTableBlock *block = [[NSTextTableBlock alloc]
                               initWithTable:table
                               startingRow:row
                               rowSpan:1
                               startingColumn:column
                               columnSpan:1];
    [block setBackgroundColor:backgroundColor];
    [block setBorderColor:borderColor];
    [block setWidth:0.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockBorder];
    [block setWidth:2.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockPadding];

    NSMutableParagraphStyle *paragraphStyle =
    [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setTextBlocks:[NSArray arrayWithObjects:block, nil]];
    [paragraphStyle setAlignment:textAlignment];
    [block release];

    NSMutableAttributedString *cellString =
    [[NSMutableAttributedString alloc] initWithString:string];
    [cellString addAttribute:NSParagraphStyleAttributeName
                       value:paragraphStyle
                       range:NSMakeRange(0, [cellString length])];
    [cellString addAttribute:NSForegroundColorAttributeName
                       value:[NSColor whiteColor]
                       range:NSMakeRange(0, [cellString length])];
    [paragraphStyle release];

    return [cellString autorelease];
}

然后我只是将它附加到文本视图中:

[printText.textStorage setAttributedString:[self printTitleTableAttributedString]];
于 2010-09-24T11:21:05.390 回答
2

NSTextView 的 HTML 支持只是初级的。它不会连接到 WebKit 或任何渲染完整 HTML 的东西。

我所知道的唯一方法甚至接近完成您尝试使用的NSTextView方法是制表位。您可以在行的段落样式中添加NSTextTab一个对齐方式。NSCenterTextAlignment然后使用制表符将左对齐的文本与居中对齐的文本分开。

最大的问题是,每次文本视图的大小发生变化时,您都必须计算文本视图的中心点并在该位置创建制表位。

我想你也可以 subclass NSLayoutManager,但这可能比你预期的更严厉。

于 2010-09-21T14:31:39.490 回答