我创建了一个自定义UILabel来显示令人讨厌的文本,但是它始终将文本显示为黑色...当我在视图控制器上使用相同的代码时,它可以正常工作!
import UIKit
class GradientLabel: UILabel {
override func awakeFromNib() {
super.awakeFromNib()
let gredient = GradientView.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
gredient.bottomColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
gredient.topColor = #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)
setTextColorToGradient(image: imageWithView(view: gredient)!)
}
func imageWithView(view: UIView) -> UIImage? {//bet7awel uiview to image
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
func setTextColorToGradient(image: UIImage) {//beta7'od image we tekteb beha el text fe el label
UIGraphicsBeginImageContext(frame.size)
image.draw(in: bounds)
let myGradient = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
textColor = UIColor(patternImage: myGradient!)
}
}
答案 0 :(得分:0)
您应该init
而不是awakeFromNib
标签,因为您没有在标签上使用xib
class GradientLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
和扩展名非常适合使您的代码可重用
extension UILabel {
func setTextColorToGradient(_ image: UIImage) {
UIGraphicsBeginImageContext(frame.size)
image.draw(in: bounds)
let myGradient = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
textColor = UIColor(patternImage: myGradient!)
}
}
用法:
label.setTextColorToGradient(UIImage(named:"someImage")!)
答案 1 :(得分:0)
您不想依靠awakeFromNib
。此外,您也确实不想在init
中执行此操作,因为您希望能够响应大小变化(例如,如果您有约束力,并且在标签第一次放置后frame
发生了变化)创建)。
相反,请从layoutSubviews
更新您的渐变,只要视图的frame
发生更改,就会调用该渐变:
@IBDesignable
class GradientLabel: UILabel {
@IBInspectable var topColor: UIColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1) {
didSet { setNeedsLayout() }
}
@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1) {
didSet { setNeedsLayout() }
}
override func layoutSubviews() {
super.layoutSubviews()
updateTextColor()
}
private func updateTextColor() {
let image = UIGraphicsImageRenderer(bounds: bounds).image { _ in
let gradient = GradientView(frame: bounds)
gradient.topColor = topColor
gradient.bottomColor = bottomColor
gradient.drawHierarchy(in: bounds, afterScreenUpdates: true)
}
textColor = UIColor(patternImage: image)
}
}
结果是:
注意,
我使用的是新的UIGraphicsImageRenderer
,而不是UIGraphicsBeginImageContext
,UIGraphicsGetImageFromCurrentImageContext
和UIGraphicsEndImageContext
。
我只使用CGRect
,而不是手动构建bounds
。
我制作了@IBDesignable
,以便可以直接在Interface Builder中使用它。我还制作了颜色@IBInspectable
,因此您可以直接在IB中调整颜色,而不必编写代码。显然,只有在要查看IB中呈现的渐变效果时,才需要这样做。
当标签需要重新放置时,我已经使它更新了渐变(a);和(b)每当您更改任何一种颜色时。
对于它的价值,这是我在此示例中使用的GradientView
:
@IBDesignable
class GradientView: UIView {
override class var layerClass: AnyClass { return CAGradientLayer.self }
private var gradientLayer: CAGradientLayer { return layer as! CAGradientLayer }
@IBInspectable var topColor: UIColor = .white { didSet { updateColors() } }
@IBInspectable var bottomColor: UIColor = .blue { didSet { updateColors() } }
override init(frame: CGRect = .zero) {
super.init(frame: frame)
updateColors()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateColors()
}
private func updateColors() {
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
}
}
在这种情况下,因为我们将layerClass
设置为渐变,所以我们要做的就是在init
期间对其进行配置,而基础layerClass
将负责响应大小变化。
或者,您也可以使用CoreGraphics绘制渐变:
@IBDesignable
class GradientLabel: UILabel {
@IBInspectable var topColor: UIColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1) {
didSet { setNeedsLayout() }
}
@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1) {
didSet { setNeedsLayout() }
}
override func layoutSubviews() {
super.layoutSubviews()
updateTextColor()
}
private func updateTextColor() {
let image = UIGraphicsImageRenderer(bounds: bounds).image { context in
let colors = [topColor.cgColor, bottomColor.cgColor]
guard let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil) else { return }
context.cgContext.drawLinearGradient(gradient,
start: CGPoint(x: bounds.midX, y: bounds.minY),
end: CGPoint(x: bounds.midX, y: bounds.maxY),
options: [])
}
textColor = UIColor(patternImage: image)
}
}
这与第一个示例相同,但可能会更有效。