Add CATextLayer on Top of NSImageView

蓝咒 提交于 2020-05-15 21:35:40

问题


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;

This keeps producing empty NSImageView


回答1:


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)


来源:https://stackoverflow.com/questions/61309916/add-catextlayer-on-top-of-nsimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!