I'm trying to draw underlined strings, but they come out wrong when the font is bold. So I made a sample app from the Cocoa template, and added a custom view to the main window. The code is below. But the output doesn't underline correctly. What am I doing wrong?






NSTextStorage* getTextStorage() {

        if( !s_textStorage ){

                s_textStorage = [[NSTextStorage alloc] init];
                NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
                NSTextContainer *textContainer = [[NSTextContainer alloc] init];
                [layoutManager addTextContainer:textContainer];
                [textContainer release];
                [s_textStorage addLayoutManager:layoutManager];
                [layoutManager release];
        }
        return s_textStorage;
}

- (void)drawRect:(NSRect)rect {

    int fontSize = 41;
    NSFontManager* fontManager = [NSFontManager sharedFontManager];
NSFont* boldFont = [fontManager fontWithFamily:@"Arial" traits:0 weight:5 size:fontSize]; // boldFont = [fontManager convertFont:font toHaveTrait:NSBoldFontMask];


NSMutableDictionary* attr = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; [attr setObject:[NSNumber numberWithInt:NSSingleUnderlineStyle] forKey:NSUnderlineStyleAttributeName];

NSAttributedString* attrString = [[NSAttributedString alloc] initWithString:@"Sample Text" attributes:attr];

    NSTextStorage* textStorage = getTextStorage();
    [textStorage setAttributedString:attrString];

NSLayoutManager *layoutManager = (NSLayoutManager *)[[textStorage layoutManagers] objectAtIndex:0]; NSTextContainer *textContainer = (NSTextContainer *) [[layoutManager textContainers] objectAtIndex:0];

    NSPoint point = NSMakePoint( 10, 10);
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; [[textContainer layoutManager] drawGlyphsForGlyphRange: glyphRange atPoint:point ];
}

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to