Frage

I'm quite new to OS-X/Cocoa development, and I'm having some trouble with CTDrawLine. I can happily change the stroke of the text using CGContextSetStrokeXXX variants (I can even use patterns) when drawing with kCGTextStroke, but when I set the mode to kCGTextFill, the fill style of the current context is ignored, and the text is rendered black. Here's some code that demonstrates the problem.

-(void) drawRect: (NSRect)rect {
    CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];


    float x = 50;
    float y = 50;

    CTFontRef font = CTFontCreateWithName(CFSTR("Arial"), 74, NULL);

    CFStringRef stringRef = CFStringCreateWithCString(NULL, "Hello SO", kCFStringEncodingUTF8);
    CFStringRef keys[] = { kCTFontAttributeName };
    CFTypeRef values[] = { font };
    CFDictionaryRef attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
                                                        (const void**)&values, sizeof(keys)/sizeof(keys[0]),
                                                        &kCFTypeDictionaryKeyCallBacks,
                                                        &kCFTypeDictionaryValueCallBacks);

    CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, stringRef, attributes);
    CTLineRef line = CTLineCreateWithAttributedString(attrString);

    CGContextSetTextPosition(context, x, y);
    CGContextSetTextDrawingMode(context, kCGTextFill);

    // why doesn't this work?
    CGContextSetRGBFillColor(context, 1, 0, 0, 1);
    CGContextFillRect(context, CGRectMake(0, 0, 200, 200));
    CTLineDraw(line, context);

    CFRelease(stringRef);
    CFRelease(attributes);
    CFRelease(line);
    CFRelease(attrString);
    CFRelease(font);
}

I expect the text to be red, with a red square beneath it (the text would not show up against this background). Instead I see a red square with black text.

The incredibly detailed documentation for CTDrawLine says: CTLineDraw Draws a complete line.

There are no obvious alternative routes to specify the fill style in the reference for CoreText or Quartz. The only way I can get the desired text fill style is by using kCGTextClip and filling a rectangle over the area, which is stupid.

I'm testing on OS-X 10.9 and building with XCode 5, with C++11 language extensions, and this is in a .mm file (although I doubt this would be causing the issue).

What am I missing?

Addendum:

It's probably worth noting my desired end result is to be able to render this text filled with patterns as well as solid colours.

War es hilfreich?

Lösung

After a bit more fiddling around I found you must add the kCTForegroundColorFromContextAttributeName, https://developer.apple.com/library/ios/documentation/Carbon/Reference/CoreText_StringAttributes_Ref/Reference/reference.html

CFStringRef keys[] = { kCTFontAttributeName, kCTForegroundColorFromContextAttributeName };
CFTypeRef values[] = { font, kCFBooleanTrue };
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top