Any solutions to this?
Post
Replies
Boosts
Views
Activity
Any solutions yet?
Any solutions for this?
Finally I figured that by restarting the phone the problem goes away! As weird as it sounds :)
Here is what I used which doesn't require images directly:
class GradientLabel: UILabel {
var gradientColors: [CGColor] = []
var gradientLocations: [CGFloat] = []
override func drawText(in rect: CGRect) {
self.textColor = drawGradientColor(in: rect, colors: gradientColors, locations: gradientLocations)
super.drawText(in: rect)
}
private func drawGradientColor(in rect: CGRect, colors: [CGColor], locations: [CGFloat]) -> UIColor? {
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.saveGState()
defer { currentContext?.restoreGState() }
let size = rect.size
UIGraphicsBeginImageContextWithOptions(size, false, 0)
guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
colors: colors as CFArray,
locations: locations) else { return nil }
let context = UIGraphicsGetCurrentContext()
context?.drawLinearGradient(gradient,
start: CGPoint(x: 0.0, y: 0.0),
end: CGPoint(x: size.width, y: 0.0),
options: [])
let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let image = gradientImage else { return nil }
return UIColor(patternImage: image)
}
}
After implementing you set the gradientColors and gradientLocations properties. You can change the gradient direction by setting "start:" and "end:" in context?.drawLinearGradient