I would like to convert text with attributes (i.e., a specific font and size) directly into a CIImage—that is, without drawing it to screen first—so that I can use a custom CIFilter to dynamically alter the text's appearance. How can I do this?
Here's how to draw a NSAttributedString
onto a NSImage
. I didn't test the conversion to CIImage
though (the last line), but it shouldn't be too difficult:
let string = NSAttributedString(string: "Hello World!", attributes: [NSFontAttributeName: NSFont.labelFontOfSize(10)])
let image = NSImage(size: string.size())
image.lockFocus()
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.currentContext()!.shouldAntialias = true
string.drawAtPoint(NSZeroPoint)
NSGraphicsContext.restoreGraphicsState()
image.unlockFocus()
let ciimage = CIImage(data: image.TIFFRepresentation!)!