gpt4 book ai didi

iOS Swift 在 CALayer 中渲染标签的问题

转载 作者:行者123 更新时间:2023-11-30 13:56:22 24 4
gpt4 key购买 nike

我正在尝试在 CALayer 中渲染标签,并且图层 iconLayer 的背景被放置在标签的顶部。

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

}

下面是带有背景的 View 的样子。

background over label

下面是删除背景后的 View 。

background removed

图标似乎没有添加到图层 iconLayer 中,只是添加到了上下文中。

最佳答案

我最终用以下代码修复了它。

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

}

关于iOS Swift 在 CALayer 中渲染标签的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33571999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com