0

I am drawing text on top of a solid background color, and while it is appearing, the NSTextEffectLetterpressStyle effect applied to the attributed string isn't showing up - you can only see the text color not the additional white outline that provides the desired look. It looks essentially the exact same as if I didn't apply the letterpress effect. Why is this, how can I draw the letterpress text effect correctly?

let bitmapContext = CGBitmapContextCreate(nil, UInt(imageRect.width), UInt(imageRect.height), UInt(8), UInt(imageRect.width * 16), CGColorSpaceCreateDeviceRGB(), CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue))

CGContextSetFillColorWithColor(bitmapContext, UIColor.blackColor().CGColor)
let fullBox = CGRectMake(0, 0, imageRect.width, imageRect.height)
CGContextAddRect(bitmapContext, fullBox)
CGContextFillPath(bitmapContext)

CGContextSetTextDrawingMode(bitmapContext, kCGTextFill)
CGContextSetTextPosition(bitmapContext, drawPoint.x, drawPoint.y)
let coloredAttributedString = NSAttributedString(string: "20", attributes:[NSFontAttributeName: myFont, NSForegroundColorAttributeName: textColor, NSTextEffectAttributeName: NSTextEffectLetterpressStyle])
let displayLineTextColored = CTLineCreateWithAttributedString(coloredAttributedString)
CTLineDraw(displayLineTextColored, bitmapContext)

let cgImage = CGBitmapContextCreateImage(bitmapContext)
var myImage = CIImage(CGImage: dateStampCGImage)

This is the result:
enter image description here

This is what I expected it to be (notice the white-ish outline):
enter image description here

Jordan H
  • 52,571
  • 37
  • 201
  • 351

1 Answers1

0

Both CFAttributedString and NSAttributedString can apply arbitrary attributes to ranges. The attributed string types don't inherently interpret the attributes.

Rather, the string drawing technologies interpret the attributes. Core Text understands a different set of attributes than UIKit or AppKit. The set of attributes supported by Core Text is documented here. It does not list (an equivalent of) NSTextEffectAttributeName.

If you set up a current graphics context using UIGraphicsBeginImageContextWithOptions() and draw into that using the NSAttributedString drawing methods, that should work, since it's using UIKit to do the drawing.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thanks Ken, I did previously use a graphics context in order to obtain this effect but that is more memory intensive than using a bitmap context. I need high performance in this case. Do you see any other solution avoiding using a graphics context? – Jordan H May 02 '15 at 19:58
  • They're both graphics contexts. How did you determine that one is more memory intensive than the other? – Ken Thomases May 02 '15 at 20:17
  • You may also be able to have UIKit render to the Core Graphics bitmap context by pushing it using `UIGraphicsPushContext()`. The important thing is that UIKit has an implicit "current" context used by its drawing operations. So, if you want to use UIKit drawing, you need to arrange for its current context to be one of your choosing. – Ken Thomases May 02 '15 at 20:23
  • After conversing with Apple developer technical support it was recommended I switch to using `CGBitmapContext` instead of `UIGraphicsBeginImageContext` in order to avoid running into memory constraints, and it worked great I just lost the visual effect. Not sure I understand what you mean by pushing the context but I'll look into it. – Jordan H May 02 '15 at 20:36