NSTextView - using initWithHTML with tables

I am trying to format the text in NSTextViewto fit on the same line with a black background. One part of the text should be aligned to the left, the other part (but still on the same line) should be centered.

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]];

Unfortunately, no matter what width I specify NSTextView, it seems to ignore the width of the table.

Any workarounds, different approaches or other suggestions?


Additional information on the background of the problem.

I wrote an application for writing tasks for programming contests. Each task author presents content in a different format, so getting standardization in a simple package will be of great benefit.

I need this for the print module. I take a few NSTextViewand sew them together.

, : 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    |
+3
2

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]];
+3

NSTextView HTML . WebKit -, HTML.

, , , NSTextView, - . NSTextTab NSCenterTextAlignment . , .

, , .

, NSLayoutManager, , , , .

+2

Source: https://habr.com/ru/post/1765768/


All Articles