3

Ce que je veux faire: mettre en page du texte en utilisant NSLayoutManager et définir son NSTextContainer à la largeur de la chaîne la plus large (glyphe) dans un tableau de chaînes.Comment déterminer correctement la largeur d'une chaîne affectée

Quel est mon problème? Les méthodes pour déterminer la 'glyph-width' totale semblent être incorrectes parce que quand je rends le texte il enveloppe. J'ai effectué une expérience en utilisant une chaîne de 32 caractères avec la police Monaco 12 points et la longueur est signalée comme 224.0, mais le texte ne s'arrêtera que si la longueur est définie sur 234.0.

Ce code illustre ce que j'ai dit ci-dessus et montre une ligne verticale sur la droite de la largeur de glyphe calculée.

- (void)drawRect:(NSRect)rect { 
    NSRect bounds = [self bounds]; 

    [[NSColor whiteColor] drawSwatchInRect: bounds]; 
    [[NSColor blackColor] setStroke]; 
    [[NSBezierPath bezierPathWithRect: bounds] stroke]; 

    NSMutableParagraphStyle *thisParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [thisParagraphStyle setLineBreakMode: NSLineBreakByCharWrapping]; 
    [thisParagraphStyle setAlignment: NSLeftTextAlignment]; 

    NSFont *fontUsed = [NSFont fontWithName: @"Monaco" size: 12]; 

    NSDictionary *glyphAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
            fontUsed, NSFontAttributeName, 
            thisParagraphStyle, NSParagraphStyleAttributeName, 
            [NSColor blackColor], NSForegroundColorAttributeName, 
            NULL]; 

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n00112233445566778899001122334455\n"]; 

    [textStorage setAttributes: glyphAttributes range: NSMakeRange(0, [textStorage length])]; 

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; 

    NSSize textContainerSize; 
    textContainerSize.width = [@"00112233445566778899001122334455" sizeWithAttributes: glyphAttributes].width; 
    textContainerSize.height = bounds.size.height; 

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize: textContainerSize]; 
    [layoutManager addTextContainer:textContainer]; 

    [textContainer release]; 
    [textStorage addLayoutManager:layoutManager]; 
    [layoutManager release]; 
    NSRange glyphRange = [layoutManager glyphRangeForTextContainer: textContainer]; 

    [layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: NSMakePoint(0.0 , 0.0)]; 
    [textStorage release]; 
    [thisParagraphStyle release]; 

    // Indicate right text boundary from computed width 
    NSBezierPath *rightTextBoundary = [NSBezierPath bezierPath]; 
    [rightTextBoundary moveToPoint: NSMakePoint(textContainerSize.width, 0.0)]; 
    [rightTextBoundary lineToPoint: NSMakePoint(textContainerSize.width, bounds.size.height-1)]; 
    [rightTextBoundary stroke]; 

    NSLog(@"View width: %f", bounds.size.width); 
    NSLog(@"Calculated width1: %f", textContainerSize.width); 
    NSLog(@"Calculated width2: %f\n\n", [@"00112233445566778899001122334455" boundingRectWithSize: NSMakeSize(FLT_MAX, FLT_MAX) 
                     options: NSStringDrawingUsesDeviceMetrics 
                     attributes: glyphAttributes].size.width); 
} 

- (BOOL) isFlipped { 
    return YES; 
} 

Répondre

1

Avez-vous jeté un oeil à cette catégorie pour NSString et NSAttributedString NS(Attributed)String+Geometrics?

Si ce n'est pas exactement ce que vous cherchez, vous pourriez peut-être voir ce qu'ils font pour calculer la taille.

Questions connexes