Re: "Toll free" bridge from NSAttributedString * to CFAttributedStringRef
Re: "Toll free" bridge from NSAttributedString * to CFAttributedStringRef
- Subject: Re: "Toll free" bridge from NSAttributedString * to CFAttributedStringRef
- From: "Adam R. Maxwell" <email@hidden>
- Date: Thu, 08 Apr 2010 21:18:41 -0700
On Apr 8, 2010, at 5:02 AM, vincent habchi wrote:
> I've been fiddling with NSAttributedString lately, and the way to display them through Core Text. In the docs, I've read that NSAttributedString and CFAttributedStringRef were supposed to be "toll free" bridged; I assumed that meant that one could be used in lieu of the other with (or maybe even without) a simple cast.
>
> But I found this is not true. Specifically, I was getting strange errors like "CFSet[Stroke|Fill]ColorFromColor : invalid context 0x0" each time I printed my NSAttributedString by CTLineDraw(), although I had a perfectly valid context. The glyph were drawn, but always in black, while I was attempting to get another color via the NSForegroundColorAttributeName. At the end, I had to replace the pair (NSColor, NSForeground…) by a (CGColorRef, kCTForeground…), and now it works.
>
> Is that supposed to be a "normal" behavior, did I misunderstand the meaning of "toll free", or is it a bug?
You need to have a current NSGraphicsContext for NSColor (and possibly other) attributes. This is pretty easy, as in the following snippet from my own code:
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctxt flipped:NO]];
CTFrameDraw(frame, ctxt);
CGContextFlush(ctxt);
CFRelease(frame);
[NSGraphicsContext restoreGraphicsState];
If this works, you can thank David Duncan for the fix: http://lists.apple.com/archives/quartz-dev/2008/Jun/msg00043.html
hth,
Adam
_______________________________________________
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