2016-10-17 6 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

예, Yahoo!

답변입니다.

myTextLayer.preferredFrameSize()