简体   繁体   English

在绘制到CGBitmapContext时丢失NSTextEffectLetterpressStyle

[英]Losing NSTextEffectLetterpressStyle when drawing into CGBitmapContext

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. 我在纯色背景上绘制文本,当它出现时,应用于属性字符串的NSTextEffectLetterpressStyle效果没有显示 - 您只能看到文本颜色而不是提供所需外观的附加白色轮廓。 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. CFAttributedStringNSAttributedString都可以将任意属性应用于范围。 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. Core Text了解与UIKit或AppKit不同的一组属性。 The set of attributes supported by Core Text is documented here . 此处记录了Core Text支持的属性集。 It does not list (an equivalent of) NSTextEffectAttributeName . 它没有列出(相当于) 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. 如果您使用UIGraphicsBeginImageContextWithOptions()设置当前图形上下文并使用NSAttributedString绘制方法绘制,那应该可以,因为它使用UIKit进行绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM