I wrote this code, works well; but I need to figure out the size of the text in the CATextLayer to finish it? The idea I use from a tap gesture to get the x/y, enter the text and have it figure out the CGSize needed to draw it in the CATextLayer object/view.
overload func ViewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
container.addGestureRecognizer(tap)
}
func handleTap(gesture: UITapGestureRecognizer) {
let location = gesture.location(in: gesture.view)
startX = location.x
startY = location.y
drawText(onLayer: view.layer, fromPoint: CGPoint(x: startX, y: startY), toPoint: CGPoint(x:location.x, y:location.y))
}
func drawText(onLayer layer: CALayer, fromPoint start: CGPoint, toPoint end:CGPoint) {
let myTextLayer = CATextLayer()
myTextLayer.string = "Google"
myTextLayer.backgroundColor = UIColor.black.cgColor
myTextLayer.foregroundColor = UIColor.white.cgColor
//myTextLayer.frame = view.bounds
let myBounds = CGRect(origin: start, size: CGSize(width: 128, height: 32))
myTextLayer.frame = myBounds
layer.addSublayer(myTextLayer)
}
Yes, Yahoo!
This is the answer!
myTextLayer.preferredFrameSize()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With