2010-09-21 5 views
3

J'essaye de formater du texte en NSTextView pour le placer sur une seule ligne avec un arrière-plan noir. Un morceau de texte doit être aligné à gauche, l'autre morceau (mais toujours dans la même ligne) doit être centré.NSTextView - Utilisation de initWithHTML avec des tables

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

Malheureusement, peu importe ce que la largeur je précise, NSTextView semble ignorer la largeur de la table.

Des solutions de contournement, des approches différentes ou d'autres suggestions?


Plus d'informations sur l'arrière-plan du problème. J'ai écrit une application pour écrire des tâches pour les concours de programmation. Chaque auteur de tâche soumet le contenu dans un format différent, donc les amener à le standardiser dans un ensemble simple serait un grand avantage.

J'ai besoin de cela pour un module d'impression. Je prends plusieurs NSTextView s et je les couds ensemble.

Ces concours ont la mise en forme de document suivant que nous sommes censés suivre - 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 | 

Répondre

3

Modification de 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]; 
} 

Alors je viens de l'ajouter dans le textview:

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

support HTML de NSTextView est rudimentaire. Il ne se connecte pas dans WebKit ou quoi que ce soit pour afficher du HTML complet. La seule façon dont je sais que cela est même proche de l'accomplissement de ce que vous essayez de faire avec un NSTextView est des arrêts de tabulation. Vous pouvez ajouter un NSTextTab avec un alignement de NSCenterTextAlignment au style de paragraphe de la ligne. Utilisez ensuite un onglet pour séparer le texte aligné à gauche du texte aligné au centre.

Le plus gros problème avec ceci est que vous devez calculer le point central de votre vue de texte et créer votre tab stop à cet endroit chaque fois que la taille de votre vue de texte change. Je suppose que vous pourriez également sous-classer NSLayoutManager, mais c'est probablement plus lourd que vous attendiez.

+0

qu'il ne supporte pas beaucoup de HTML, tant que j'ai compris - en fait J'étais vraiment surpris qu'il supporte CSS. Chose étrange, j'ai un document HTML qui fait la même chose, et TextEdit l'ouvre beaucoup plus bien. Je vais jeter un coup d'oeil à votre suggestion; J'essaie de préparer le document pour l'impression, donc je ne suis pas sûr que ce soit approprié, si c'est spécifique à l'écran. –

+0

J'ai trouvé un exemple de code dans les documents d'Apple pour la création de tables dans NSAttributedStrings. Je n'avais pas besoin de sous-classer NSLayoutManager :-) Je vais coller mon code dans une autre réponse. –

Questions connexes