2010-09-21 20 views
3

我試圖格式化NSTextView中的一些文本,將其放置在黑色背景的單行中。一段文本需要左對齊,另一段文本(但仍在同一行)需要居中。NSTextView - 在表格中使用initWithHTML

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並將它們拼接在一起。

這些比賽有哪些我們應該遵循以下文檔格式 - Example PDFGoogleDocs) :

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

改性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]; 
} 

然後,我只是追加入的TextView:

[printText.textStorage setAttributedString:[self printTitleTableAttributedString]]; 
2

NSTextView的HTML的支持僅僅是初步的。它不會掛鉤到WebKit或任何呈現完整HTML的東西。

我知道的唯一途徑就是完成你想用NSTextView做的事情,就是製表位。您可以將一個NSTextTabNSCenterTextAlignment對齊添加到該行的段落樣式。然後使用選項卡將左對齊文本與中間對齊文本分開。

最大的問題在於,您必須計算文本視圖的中心點,並在每次文本視圖大小發生更改時在該位置創建制表位。

我想你也可以繼承NSLayoutManager,但這可能比你期待的更爲沉重。

+0

它不支持多HTML的,這麼多,我已經收集 - 事實上我真的很驚訝它支持CSS。奇怪的是,我有一個HTML文檔做同樣的事情,TextEdit打開它更好。我會看看你的建議;我正在準備打印文檔,所以我不確定這是否合適,如果它是特定於屏幕的。 – 2010-09-21 15:03:18

+0

我在Apple的文檔中找到了編程方式在NSAttributedStrings中創建表格的示例代碼。我不需要NSLayoutManager的子類:-)我會在另一個答案中粘貼我的代碼。 – 2010-09-24 11:18:45