0

How can you make an NSTextView layer backed? I know you can make an NSView layer backed by setting the NSView.wantsLayer property to true. When I do this on an NSTextView the text it normally displays is not visible.

This is my swift (playground) code:

import Cocoa
import QuartzCore

let tv = NSTextView(frame: NSRect(x: 0, y: 0, width: 100, height: 50))
tv.string = "Hello world."
tv.wantsLayer = true

The property tv.layer returns an NSTextViewBackingLayer, which sounds at least something related to displaying text.

What is going wrong here?

Thanks in advance.

Cam
  • 11
  • 2
  • Why would you want to do that? – El Tomato Dec 02 '18 at 03:18
  • It works in a Cocoa project. Don't know why it doesn't work in Playground – Code Different Dec 02 '18 at 03:51
  • Do you use the same code in the Cocoa project? Does the text view have a layer before `tv.wantsLayer = true`? – Willeke Dec 02 '18 at 09:35
  • I wanted to add a mask to the `NSTextView.layer.mask` property, to create a truncating fade out effect on the end of the string. I use the same code in a cocoa project, where it is also not working. `tv.layer` returns nil before `tv.wantsLayer = true`. – Cam Dec 02 '18 at 10:48
  • @CodeDifferent it doesn't work in a Cocoa project. Add a text view to a XIB/storyboard and check the Core Animation Layer checkbox. The width of the frame is 0.0 in the nib. Any ideas? – Willeke Dec 03 '18 at 00:45
  • `tv.layer` returned also all kinds of zero sized frames and bounds. Before you set `tv.wantsLayer = true` the `tv.frame` and `tv.bounds` properties return both a correct size of 100x50. After setting `tv.wantsLayer = true` the properties are changed to zeros. So if we try to set the correct sizes again in combination with a `tv.layerContentsRedrawPolicy` set to the default value `NSView.LayerContentsRedrawPolicy.duringViewResize`, then `tv.layer.contents` will be updated when we change these geometry properties. – Cam Dec 03 '18 at 23:14
  • I had to set both `tv.frame = NSRect(x: 0, y: 0, width: 100, height: 50)` and `tv.minSize = NSSize(width: 100, height: 50)`. After this a correct layer is displayed. This is quite cumbersome and I'm not sure why this works. It seems like something goes wrong when enabling layer support. – Cam Dec 03 '18 at 23:14

0 Answers0