Add text to CALayer

前端 未结 7 1986
Happy的楠姐
Happy的楠姐 2020-11-29 19:28

Is it possible to add a UILabel to a CALayer without subclassing and drawing it in drawInContext:?

Thanks!

相关标签:
7条回答
  • 2020-11-29 20:13

    The answers below are fine, just make sure you add otherwise you text will be blurry:

    textLayer.contentsScale = UIScreen.main.scale
    

    Final code for Swift:

    let textLayer = CATextLayer()
    textLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 15)
    textLayer.fontSize = 12
    textLayer.string = "my text"
    textLayer.foregroundColor = UIColor.red.cgColor
    textLayer.contentsScale = UIScreen.main.scale
    
    0 讨论(0)
提交回复
热议问题