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:
This is what I expected it to be (notice the white-ish outline):
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.