Abhilfe gefunden. Nichts in Quartz kann scheinbar Text mit Sub-Pixel Aliasing über einem transparenten Hintergrund darstellen. Sie können jedoch Text in einen Bitmap-Puffer im Offscreen-Format rendern, vorausgesetzt, dass der Offscreen-Bitmap-Puffer in der richtigen Weise erstellt wurde. Der Hintergrund dieses Puffers muss undurchsichtig sein.
Meine Ansicht hatte zuvor einen PNG-Hintergrund, der leicht transparent war. Ich hätte diesen Hintergrund einfach ohne Probleme opak und gerendert machen können, aber da diese Ansicht ein- und ausgeblendet werden muss, musste sie CALayer-backed sein, so dass der Text einmal richtig gerendert und anschließend ohne Sub-Pixel Aliasing gerendert wird .
Hier ist mein Code. Es scheint unglaublich wortreich, ich würde es lieben, wenn mir jemand helfen könnte, es abzuschwächen. Es wird davon ausgegangen, dass Sie eine NSImageView namens _title
und eine NSString namens title
haben.
// Create the attributed string
NSMutableAttributedString *attStr = [[[NSMutableAttributedString alloc] initWithString: title] autorelease];
NSRange strRange = NSMakeRange(0, [attStr length]);
NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize: NSSmallControlSize]];
[attStr addAttribute: NSForegroundColorAttributeName value: [NSColor whiteColor] range: strRange];
[attStr addAttribute: NSFontAttributeName value: font range: strRange];
NSMutableParagraphStyle *paraStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[paraStyle setAlignment: NSCenterTextAlignment];
[paraStyle setLineBreakMode: NSLineBreakByTruncatingMiddle];
[attStr addAttribute: NSParagraphStyleAttributeName value: paraStyle range: strRange];
// Set up the image context
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * [_title frame].size.width;
NSUInteger bitsPerComponent = 8;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// These constants are necessary to enable sub-pixel aliasing.
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
// Create the memory buffer to be used as the context's workspace
unsigned char *contextBuffer = malloc([_title frame].size.height * [_title frame].size.width * 4);
CGContextRef context = CGBitmapContextCreate(contextBuffer,
[_title frame].size.width,
[_title frame].size.height,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
CGContextSetFillColorWithColor(context, CGColorGetConstantColor(kCGColorBlack));
CGRect rectangle = CGRectMake(0, 0, [_title frame].size.width,[_title frame].size.height);
CGContextAddRect(context, rectangle);
CGContextFillPath(context);
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attStr);
CGContextSetTextPosition(context, 10.0, 10.0);
CTLineDraw(line, context);
CFRelease(line);
// Create a data provider from the context buffer in memory
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, contextBuffer, bytesPerRow * [_title frame].size.height, NULL);
// Create an image from the data provider
CGImageRef imageRef = CGImageCreate ([_title frame].size.width,
[_title frame].size.height,
bitsPerComponent,
bytesPerPixel * 8,
bytesPerRow,
colorSpace,
bitmapInfo,
dataProvider,
NULL,
false,
kCGRenderingIntentDefault
);
// Turn it into an NSImage
NSImage *newImage = [[[NSImage alloc] initWithCGImage:imageRef size: NSZeroSize] autorelease];
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGImageRelease(imageRef);
free(contextBuffer);
[NSGraphicsContext restoreGraphicsState];
[_title setImage: newImage];
Konnten Sie den Code bekanntgeben, der verwendet wurde, um die NSTextView Schicht zu ändern? –
Es gibt sehr wenig tatsächlichen Code, da ich hauptsächlich Dinge in Interface Builder mache. Es gibt ein NSView, das ein NSImageVie –
steuert, das ein NSImageView mit einem halbtransparenten PNG steuert. Der Text sitzt darüber. –