【问题标题】:Figuring out the size of the textBox in a CATextLayer计算 CATextLayer 中文本框的大小
【发布时间】:2016-10-18 01:50:25
【问题描述】:

我写了这段代码,运行良好;但我需要弄清楚 CATextLayer 中文本的大小才能完成它?我从点击手势中使用的想法来获取 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)
}

【问题讨论】:

标签: ios swift graphics calayer catextlayer


【解决方案1】:

是的,雅虎!

这就是答案!

myTextLayer.preferredFrameSize()

【讨论】:

  • 对于整个sn-p,我想你需要阅读原帖。
  • CATextLayer 为多行时不返回大小。
猜你喜欢
  • 1970-01-01
  • 2018-10-25
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多