I am attempting to draw text to a UIView using CATextLayer and so far nothing is showing up. I must be missing something simple.
private func drawText(string: String, at: CGPoint, withFont: CGFont, fontColor: CGColor, fontSize: CGFloat) -> CATextLayer {
let textLayer = CATextLayer()
textLayer.string = string
textLayer.font = withFont
textLayer.fontSize = fontSize
textLayer.alignmentMode = .center
textLayer.contentsScale = UIScreen.main.scale
textLayer.position = at
return textLayer
}
Function Calls
let string = String(increment * Double(6 - i))
let textLayer = drawText(string: string, at: CGPoint(x: 30, y: CGFloat((i * 19) + 18)), withFont: someCGFont, fontColor: UIColor.black.cgColor, fontSize: 10)
chart.layer.addSublayer(textLayer)
You're not setting the CALayer
frame, check this test source and customize as per your requirement:
class ViewController: UIViewController {
@IBOutlet weak var viewTest: UIView!
private lazy var sampleLayer: CATextLayer = {
let layer = CATextLayer()
layer.fontSize = UIFont.systemFontSize
layer.frame = self.viewTest.frame
layer.string = "Test ABC"
layer.foregroundColor = UIColor.blue.cgColor
layer.contentsScale = UIScreen.main.scale
layer.alignmentMode = CATextLayerAlignmentMode.left
return layer
}()
override func viewDidLoad() {
super.viewDidLoad()
self.viewTest.layer.addSublayer(sampleLayer)
}
}