[英]Figuring out the size of the textBox in a CATextLayer
I wrote this code, works well; 我写了这段代码,效果很好; but I need to figure out the size of the text in the CATextLayer to finish it?
但我需要弄清楚CATextLayer中文本的大小才能完成它? 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.
我使用轻敲手势来获取x / y的想法,输入文本并让它弄清楚在CATextLayer对象/视图中绘制它所需的CGSize。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.