2015-11-06 64 views
0

我想在CALayer中呈现标签,并且将iconLayer图层的背景放置在标签顶部。iOS快速问题在CALayer中呈现标签

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{ 
     let textColor: UIColor = UIColor(red: 85/255, green: 150/255, blue: 230/255, alpha: 1) 
     let textFont: UIFont = UIFont(name: "WeatherIcons-Regular", size: 20)! 

     UIGraphicsBeginImageContext(inImage.size) 

     let layer = CALayer() 
     let iconLayer = CALayer() 

     layer.frame = CGRectMake(0, 0, inImage.size.width, inImage.size.height) 

     inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) 

     let imageSubLayer = CALayer() 
     imageSubLayer.contents = inImage.CGImage 

     let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, 30, 30) 

     iconLayer.frame = rect 
     iconLayer.cornerRadius = 15.0 
     iconLayer.backgroundColor = UIColor.whiteColor().CGColor 
     layer.renderInContext(UIGraphicsGetCurrentContext()!) 

     iconLayer.borderColor = textColor.CGColor 
     iconLayer.borderWidth = 1 


     let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
     label.text = drawText as String 
     label.font = textFont 
     label.textColor = textColor 
     label.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     iconLayer.contents = label 


     layer.addSublayer(iconLayer) 
     layer.addSublayer(imageSubLayer) 

     layer.renderInContext(UIGraphicsGetCurrentContext()!) 


     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 

     UIGraphicsEndImageContext() 

     return newImage 

    } 

以下是视图与背景的相似之处。

background over label

下面有什么看法看起来像背景去除。

background removed

看来图标没有被添加到该层iconLayer除了它刚刚加入到上下文。

回答

0

我最终修复了下面的代码。

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{ 
     let textColor: UIColor = UIColor(red: 85/255, green: 150/255, blue: 230/255, alpha: 1) 
     let textFont: UIFont = UIFont(name: "WeatherIcons-Regular", size: 20)! 

     UIGraphicsBeginImageContext(inImage.size) 

     let layer = CALayer() 
     let iconLayer = CALayer() 

     layer.frame = CGRectMake(0, 0, inImage.size.width, inImage.size.height) 

     inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) 

     let imageSubLayer = CALayer() 
     imageSubLayer.contents = inImage.CGImage 

     let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, 30, 30) 

     iconLayer.frame = rect 
     iconLayer.cornerRadius = 15.0 
     iconLayer.backgroundColor = UIColor.whiteColor().CGColor 
     layer.renderInContext(UIGraphicsGetCurrentContext()!) 

     iconLayer.borderColor = textColor.CGColor 
     iconLayer.borderWidth = 1 


     let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
     label.text = drawText as String 
     label.font = textFont 
     label.textColor = textColor 
     iconLayer.contents = label.layer 


     layer.addSublayer(iconLayer) 
     layer.addSublayer(label.layer) 
     layer.addSublayer(imageSubLayer) 

     layer.renderInContext(UIGraphicsGetCurrentContext()!) 


     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 

     UIGraphicsEndImageContext() 

     return newImage 

    }