2016-10-17 9 views
1

私はこのコードを書いています。しかし、それを完成させるために、CATextLayerのテキストのサイズを調べる必要がありますか? x/yを取得するためにタップジェスチャーから使用するアイデアをテキストに入力し、CATextLayerオブジェクト/ビューに描画するために必要なCGSizeを把握します。CATextLayerでtextBoxのサイズを調べる

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) 
} 
+0

これは役立ちますhttp://stackoverflow.com/questions/6198084/resize-catextlayer-to-fit-text-on-ios –

答えて

4

はい、

これが答えです!

myTextLayer.preferredFrameSize() 
関連する問題