3

NSTextView黒い背景で 1 行に配置されるようにテキストをフォーマットしようとしています。1 つのテキストは左揃えにする必要があり、もう 1 つのテキスト (ただし同じ行にある) は中央に配置する必要があります。

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、テーブルの幅を無視しているように見えます。

回避策、別のアプローチ、またはその他の提案はありますか?


問題の背景に関する詳細情報。

プログラミング コンテストのタスクを作成するためのアプリを作成しました。タスクの作成者はそれぞれ異なる形式でコンテンツを提出するため、単純なバンドルに標準化してもらうことは大きなメリットになります。

これは印刷モジュールに必要です。私はいくつかNSTextViewの を取り、それらをつなぎ合わせています。

これらのコンテストには、私たちが従うことになっている次のドキュメント形式があります -サンプル 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

NSAttributedString にテーブルをプログラムで追加するための Apple のサンプル コードの変更:

- (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 サポートは初歩的なものにすぎません。完全な HTML をレンダリングするために WebKit などにフックすることはありません。

NSTextView私が知っている唯一の方法は、タブストップでやろうとしていることを達成することにさえ近づいています。行の段落スタイルに をNSTextTab配置して を追加できます。NSCenterTextAlignment次に、タブを使用して、左揃えのテキストを中央揃えのテキストから分離します。

これに関する最大の問題は、テキスト ビューのサイズが変更されるたびに、テキスト ビューの中心点を計算し、その位置にタブ ストップを作成する必要があることです。

をサブクラス化することもできると思いますがNSLayoutManager、それはおそらく予想よりも手間がかかります。

于 2010-09-21T14:31:39.490 に答える