[英]How to draw a UILabel with a different blend mode in draw(_ rect: CGRect) in Swift
So I have a UILabel
that is being drawn on top of a gradient image (that is a UIImageView
). 所以我有一个在渐变图像(即
UIImageView
)上绘制的UILabel
。 It looks sorta like this: 它看起来像这样:
I'm trying to change the blendMode
of the graphics context in the UILabel
's draw(_ rect: CGRect)
function so that it draws the label but blended with the background with a softLight
blending mode. 我正在尝试在
UILabel
的draw(_ rect: CGRect)
函数中更改图形上下文的blendMode
,以便它绘制标签,但使用softLight
混合模式与背景混合。
Here is what I want it to look like: 这是我想要的样子:
Here is the code I have in the draw(_ rect: CGRect)
function: 这是我在
draw(_ rect: CGRect)
函数中的代码:
override func draw(_ rect: CGRect) {
// Drawing code
if let context = UIGraphicsGetCurrentContext() {
context.setBlendMode(.softLight)
self.textColor.set()
self.drawText(in: rect)
}
}
This code just renders the top picture and not the blended version. 此代码只显示顶部图片而不是混合版本。 This is not all that I've tried, I've tried so much other things but I just don't understand
CGContext
's and the draw
function in particular. 这不是我尝试过的所有内容,我已经尝试了很多其他的东西,但我只是不理解
CGContext
和特别是draw
函数。
Most answers on SO are either in old objective-c and are deprecated/broken or swift and too complicated (use Metal for example). 关于SO的大多数答案要么是旧的Objective-c,要么是弃用/破坏,要么是快速而且太复杂(例如使用Metal)。 I just want to draw the text in the context I have and set the context's blending mode to soft light.
我只想在我拥有的上下文中绘制文本,并将上下文的混合模式设置为柔和光。 There has to be an easy way!
必须有一个简单的方法!
Any help on how to fix this would be appreciated. 任何有关如何解决这个问题的帮助将不胜感激。 Thanks!
谢谢!
You can't blend views this way; 你不能这样混合观点; they have separate layer hierarchies that are resolved independently.
它们具有独立解析的单独层次结构。 Move the gradient and text on
CALayer
objects and blend those (or draw them by hand inside the same view). 在
CALayer
对象上移动渐变和文本并将它们混合(或在同一视图中手动绘制它们)。 For example: 例如:
class MyView: UIView {
let gradientLayer: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor,
UIColor.blue.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
return gradientLayer
}()
let textLayer: CATextLayer = {
let textLayer = CATextLayer()
let astring = NSAttributedString(string: "Text Example",
attributes: [.font: UIFont(name: "MarkerFelt-Wide", size: 60)!])
textLayer.string = astring
textLayer.alignmentMode = kCAAlignmentCenter
textLayer.bounds = astring.boundingRect(with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
return textLayer
}()
override func draw(_ rect: CGRect) {
if let context = UIGraphicsGetCurrentContext() {
gradientLayer.bounds = bounds
gradientLayer.render(in: context)
context.saveGState()
context.setBlendMode(.softLight)
context.translateBy(x: center.x - textLayer.bounds.width / 2,
y: center.y - textLayer.bounds.height / 2)
textLayer.position = center
textLayer.render(in: context)
context.restoreGState()
}
}
}
So in case your text is really a label you could try it with changing the opacity like for normal labels. 因此,如果你的文字真的是一个标签,你可以尝试改变不透明度,就像普通标签一样。 I assume you're familiar with setting an alpha to a label.
我假设您熟悉为标签设置alpha。 Please find below one example for setting an alpha for a label:
请在下面找到一个设置标签alpha的示例:
label.textColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.25)
Please give me a hint, if it's not a label - then it should be more complicated. 请给我一个提示,如果它不是标签 - 那么它应该更复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.