简体   繁体   English

CALayer中的iOS Swift问题渲染标签

[英]iOS Swift issue rendering label in CALayer

I am trying to render a Label in a CALayer and the background of the layer iconLayer is being placed on top of the Label. 我正在尝试在CALayer中渲染Label,并且将图层iconLayer的背景放置在Label的顶部。

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

    }

Below is what the view looks like with the background. 下面是带有背景的视图。

标签上的背景

Below is what the view looks like with the background removed. 下面是移除背景后的视图。

背景已删除

It seems the icon is not getting added to the layer iconLayer except it just getting added to the context. 似乎该图标没有添加到图层iconLayer而是只是添加到了上下文。

I ended up fixing it with the following code. 我最终用以下代码修复了它。

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM