• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
CTLineCreateWithAttributedString ignoring font size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CTLineCreateWithAttributedString ignoring font size


  • Subject: CTLineCreateWithAttributedString ignoring font size
  • From: Charles Srstka <email@hidden>
  • Date: Fri, 14 Aug 2009 17:44:37 -0500

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: CTLineCreateWithAttributedString ignoring font size
      • From: Alastair Houghton <email@hidden>
  • Prev by Date: Re: Apple Event parameter types in Cocoa scripting
  • Next by Date: boundingRectWithSize and the size height parameter
  • Previous by thread: Re: Apple Event parameter types in Cocoa scripting
  • Next by thread: Re: CTLineCreateWithAttributedString ignoring font size
  • Index(es):
    • Date
    • Thread