我可以将 CATextLayer 添加到 IKImageView

发布于 2024-12-28 00:59:45 字数 1926 浏览 1 评论 0原文

我几乎是 OS X 编程的新手。 我想向 IKImageView 添加文本叠加层,但我的文本没有显示。

在 IB 中,我从自定义 NSView 开始,稍后将其嵌入到窗口中。 然后我将 IKImageView 拖入其中,以便它占据大部分(但不是全部)视图。 (我计划稍后将其他控件放入视图中)

我将主 CustomView 连接到一个控制器,并将 IKImageView 连接到另一个名为 myImageController 的控制器。然后在 myImageController 中,我导入 Quartz、QuartzCore 和 AppKit,并尝试通过 awakeFromNib 添加文本(“FOO”):

- (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");
}

我尝试了 3 种不同的方法,但无法显示“FOO”。我希望它出现在图像的位置(从空开始)。从调试器输出来看,整个例程似乎都已执行。 (不会崩溃)

由于 IKImageView 是从 NSView 派生的,所以我希望能够对 NSView 做任何我能做的事情,并且我已经能够让 2 层 CATextLayer 显示在通用自定义视图中。

另外,当我尝试对 CATextLayer 执行“setNeedsDisplay”时,编译器指示 CATextLayer 可能不知道它是什么。

(我不打算使用编辑 HUD - 我应该为 IKImageView 烦恼吗?虽然这只是我试图理解视图、图层、控制器和 Interface Builder 之间的关系...)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文