繁体   English   中英

在绘制到CGBitmapContext时丢失NSTextEffectLetterpressStyle

[英]Losing NSTextEffectLetterpressStyle when drawing into CGBitmapContext

我在纯色背景上绘制文本,当它出现时,应用于属性字符串的NSTextEffectLetterpressStyle效果没有显示 - 您只能看到文本颜色而不是提供所需外观的附加白色轮廓。 它看起来基本上与我没有应用凸版印刷效果完全相同。 为什么这样,我怎样才能正确绘制凸版文字效果?

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)

这是结果:
在此输入图像描述

这就是我的预期(注意白色轮廓):
在此输入图像描述

CFAttributedStringNSAttributedString都可以将任意属性应用于范围。 属性字符串类型本身并不解释属性。

相反,字符串绘制技术解释了属性。 Core Text了解与UIKit或AppKit不同的一组属性。 此处记录了Core Text支持的属性集。 它没有列出(相当于) NSTextEffectAttributeName

如果您使用UIGraphicsBeginImageContextWithOptions()设置当前图形上下文并使用NSAttributedString绘制方法绘制,那应该可以,因为它使用UIKit进行绘图。

暂无
暂无

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

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