Okay, I'm sure the answer to this is something simple that's going to make me feel very silly once I figure it out, but for now I'm stumped. To me, the code below looks like it should work:

- (void)drawRect:(NSRect)rect {
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:@"Courier" size: 10.0], NSFontAttributeName, [NSColor blueColor], NSForegroundColorAttributeName, nil];

NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Hello, World!" attributes:attrs];

CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];

CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)string);
    CGContextSetTextPosition(context, 100.0, 100.0);
    CTLineDraw(line, context);
    CFRelease(line);
    [string release];
}

However, instead of 10 point Courier, the string ends up being drawn at 144 point. Also, the characters are all smashed together - the X- position of each letter appears to be in the place where it would be if it *were* a smaller font size, causing them all to overlap with each other. What's going on? If I use plain old [string drawAtPoint:], the string of course draws exactly as it should. But I'm trying to get CoreText to work, and according to the following page:

http://developer.apple.com/documentation/Carbon/Conceptual/CoreText_Programming/Operations/Operations.html#/ /apple_ref/doc/uid/TP40005533-CH4-DontLinkElementID_5

it looks like I should be able to do this just with CTLineCreateWithAttributedString, CGContextSetTextPosition, and CGLineDraw. However, it's not working.

I initially thought it was Cocoa's fault, and tried building attrs as a CFDictionaryRef with a CTFontRef in it instead of an NSFont. Same result. It uses the font face I specify, but uses a giant size and smashes all the characters together.

What am I doing wrong?

Thanks,
Charles
_______________________________________________

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