【问题标题】:Draw Transparent Text on NSImage在 NSImage 上绘制透明文本
【发布时间】:2018-04-02 06:09:27
【问题描述】:
我正在使用以下代码在 NSImage 上绘制 NSString。如何使此文本透明
text.draw(at: NSZeroPoint, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: NSColor.white])
如何使此文本透明?
请指教
【问题讨论】:
标签:
swift
macos
swift3
nsstring
nsimage
【解决方案1】:
您可以使用 NSColor 方法withAlphaComponent(_:) 并更改其颜色不透明度。
text.draw(at: NSZeroPoint,
withAttributes: [NSFontAttributeName: font,
NSForegroundColorAttributeName: NSColor.white.withAlphaComponent(0.5)])
【解决方案2】:
改变颜色的alpha值,例如
text.draw(at: NSZeroPoint, withAttributes: [NSFontAttributeName: font,
NSForegroundColorAttributeName: NSColor(white: 1.0, alpha: 0.3)])