我正在尝试为UILabel绘制渐变,但它只会绘制我看不到文本的颜色。我从这里看到了代码StackOverflow
我的修改:
extension String{ func gradient(x:CGFloat,y:CGFloat, fontSize:CGFloat)->UIImage{ let font:UIFont = UIFont.systemFontOfSize(fontSize) let name:String = NSFontAttributeName let textSize: CGSize = self.sizeWithAttributes([name:font]) let width:CGFloat = textSize.width // max 1024 due to Core Graphics limitations let height:CGFloat = textSize.height // max 1024 due to Core Graphics limitations UIGraphicsBeginImageContext(CGSizeMake(width, height)) let context = UIGraphicsGetCurrentContext() UIGraphicsPushContext(context!) //draw gradient let glossGradient:CGGradientRef? let rgbColorspace:CGColorSpaceRef? let num_locations:size_t = 2 let locations:[CGFloat] = [ 0.0, 1.0 ] let components:[CGFloat] = [(202 / 255.0), (197 / 255.0), (52 / 255.0), 1.0, // Start color (253 / 255.0), (248 / 255.0), (101 / 255.0), 1.0] // End color rgbColorspace = CGColorSpaceCreateDeviceRGB() glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations) let topCenter = CGPointMake(0, 0); let bottomCenter = CGPointMake(0, textSize.height); CGContextDrawLinearGradient(context, glossGradient, topCenter, bottomCenter, CGGradientDrawingOptions.DrawsBeforeStartLocation) UIGraphicsPopContext() let gradientImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return gradientImage } }
和imageView的设置
self.timeLeftIV = UIImageView(frame: CGRectMake(self.pyramidFakeView.frame.origin.x/2-25,0, 100,20)) self.timeLeftIV.image = "59:48".gradient(timeLeftIV.frame.origin.x, y: timeLeftIV.frame.origin.y, fontSize: 6.0)
代码的结果:
有一种更简单的方法来获取渐变作为UIImage。您可以使用CAGradientLayer。例如:
UIImage
CAGradientLayer
func yellowGradientImage(bounds:CGRect) -> UIImage { let gradientLayer = CAGradientLayer() gradientLayer.colors = [UIColor(red: (202 / 255.0), green: (197 / 255.0), blue: (52 / 255.0), alpha: 1.0).CGColor, UIColor(red: (253 / 255.0), green: (248 / 255.0), blue: (101 / 255.0), alpha: 1.0).CGColor] gradientLayer.bounds = bounds UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, true, 0.0) let context = UIGraphicsGetCurrentContext() gradientLayer.renderInContext(context!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image }
要将渐变应用于文本,下一步需要使用作为textColorusing 返回的图像UIColor(patternImage: ...)。例如:
textColor
UIColor(patternImage: ...)
let label = UILabel() label.font = UIFont.systemFontOfSize(150.0) label.text = "HELLO WORLD" label.sizeToFit() let image = yellowGradientImage(label.bounds) label.textColor = UIColor(patternImage: image)
结果是:
迅捷的3.0:
func yellowGradientImage(bounds:CGRect) -> UIImage { let gradientLayer = CAGradientLayer() gradientLayer.colors = [UIColor(red: (202 / 255.0), green: (197 / 255.0), blue: (52 / 255.0), alpha: 1.0).cgColor, UIColor(red: (253 / 255.0), green: (248 / 255.0), blue: (101 / 255.0), alpha: 1.0).cgColor] gradientLayer.bounds = bounds UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, true, 0.0) let context = UIGraphicsGetCurrentContext() gradientLayer.render(in: context!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } let label = UILabel() label.font = UIFont.systemFont(ofSize: 150.0) label.text = "HELLO WORLD" label.sizeToFit() let image = yellowGradientImage(bounds: label.bounds) label.textColor = UIColor(patternImage: image)