Can I add a CATextLayer to a IKImageView

352 Views Asked by At

I'm pretty much a newbie with OS X programming. I wanted to add a text overlay to an IKImageView, but my text isn't showing up.

In IB, I started with a Custom NSView that I will later embed in a window. Then I dragged an IKImageView into it so that it takes most, but not all, of the view. (I plan to put other controls in the view later)

I attached the main CustomView to one controller, and attached the IKImageView to another named myImageController. Then inside myImageController, I import Quartz, QuartzCore, and AppKit and tried to add the text ("FOO") through awakeFromNib:

- (void) awakeFromNib {
    [super awakeFromNib];

    self.view.layer = [CALayer layer];
    self.view.wantsLayer = YES;

    NSLog(@"SVH - myImageController awakeFromNib self.frame %@",NSStringFromRect(self.view.frame));
    NSLog(@"SVH - myImageController awakeFromNib self.bounds %@",NSStringFromRect(self.view.bounds));

    self.view.layer.frame = self.view.bounds;
    NSLog(@"SVH - myImageController awakeFromNib self.layer.frame %@",NSStringFromRect(self.view.layer.frame));
    NSLog(@"SVH - myImageController awakeFromNib self.layer.bounds %@",NSStringFromRect(self.view.layer.bounds));

    CATextLayer *overlayLayer = [CATextLayer layer];
    overlayLayer.frame = self.view.layer.bounds;

    [overlayLayer setFont:@"Helvetica"];
    [overlayLayer setFontSize:30.0f];
    [overlayLayer setShadowOpacity:.9f];
    [overlayLayer setString:@"FOO"];

    // TRY 1:
    // [self.view.layer addSublayer:overlayLayer];

    // TRY 2:
    // [self setOverlay:overlayLayer forType:IKOverlayTypeImage];

    // TRY 3:
    [self.view setLayer:overlayLayer];

    // Errored out when I tried this:
    // [overlayLayer setNeedsDisplay:YES];


    NSLog(@"SVH - myImageController - awakeFromNib done");
}

I tried 3 different ways, but I can't get "FOO" to show up. I expect to have it show up over the spot for the image(starts empty). From the debugger output, it looks like the whole routine gets executed. (no crashing)

Since IKImageView is derived from a NSView, I would expect to be able to do anything I could do to an NSView, and I've been able to get 2 layers of CATextLayer to show up in a generic Custom View.

Also, when I tried to do the "setNeedsDisplay" to the CATextLayer, the compiler indicated CATextLayer might not know what it is.

(I don't plan to use the editing HUD - should I even be bothering with IKImageView? Though part of this is just my trying to understand the relationship between views, layers, controllers, and Interface Builder...)

Thanks in advance!

0

There are 0 best solutions below