代码之家  ›  专栏  ›  技术社区  ›  Ivan Vučica

NSTextView-对表使用initWithHTML

  •  3
  • Ivan Vučica  · 技术社区  · 15 年前

    我正试着格式化一些文本 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]];
    

    n文本视图 似乎忽略了表格宽度。


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

    我已经为编程竞赛写了一个应用程序。每个任务作者以不同的格式提交内容,因此让他们在一个简单的包中标准化内容将是非常有益的。

    我需要这个打印模块。我拿了好几个 n文本视图 把它们缝在一起。

    这些比赛有下面的文件格式,我们应该遵循- Example 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    |
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Quinn Taylor    14 年前

    修改 Apple's sample code for programmatically adding tables to 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]];
    
        2
  •  2
  •   kperryua    15 年前

    据我所知,唯一的办法就是接近完成你想用一个 NSTextView 是制表位。您可以添加 NSTextTab 与…对齐 NSCenterTextAlignment 到行的段落样式。然后使用选项卡将左对齐的文本与中心对齐的文本分开。

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

    我想你也可以把 NSLayoutManager ,但这可能比你想象的要严厉。

    推荐文章