在NSImageView的顶部添加CATextLayer [英] Add CATextLayer on Top of NSImageView

查看:77
本文介绍了在NSImageView的顶部添加CATextLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在NSImage顶部显示文本.我使用以下代码

I'm trying to display text on top of an NSImage.I use the following code

                    let myTextLayer = CATextLayer()
                    myTextLayer.string = "My text"
                    myTextLayer.foregroundColor = NSColor.cyan.cgColor
                    myTextLayer.frame = self.img_view.bounds
                    self.img_view.layer=myTextLayer;

这将继续产生空的NSImageView

This keeps producing empty NSImageView

推荐答案

在使用图层之前,您需要明确说出NSImageView wantsLayer,如下所示

You needs explicitly say NSImageView wantsLayer before use layers, like below

self.img_view.wantsLayer = true

let myTextLayer = CATextLayer()
myTextLayer.string = "My text"
myTextLayer.foregroundColor = NSColor.cyan.cgColor
myTextLayer.frame = self.img_view.bounds

self.img_view.layer.addSublayer(myTextLayer)

这篇关于在NSImageView的顶部添加CATextLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆