Swift:使用UIBezierPath绘制文本[关闭]

问题描述 投票:-1回答:1
由于我在Google上找不到任何正确的答案,因此出现了问题:我需要draw的形状,最好使用attributed string。有什么办法可以做到这一点?
swift core-graphics nsattributedstring uibezierpath
1个回答
2
投票
您最好的方法是使用这样的东西。使用贝塞尔曲线路径可能会非常复杂,尤其是当您要绘制多个单词时。要以正确的顺序在相同的y位置绘制线条,将需要大量的定制工作和麻烦工作。下面将列出您设置的任何单词。

UIBezierPath

结果将是您定义的字符串。它将为您绘制。

只需在需要绘制图形的地方调用函数var charLayers = [CAShapeLayer]() func drawText() { for layer in self.charLayers { layer.removeFromSuperlayer() } let font = [NSAttributedString.Key.font: UIFont(name: fontNameHEre, size: FontSizeHere)! ] let attributedString = NSMutableAttributedString(string: "myStringHere", attributes: font) let charPaths = self.characterPaths(attributedString: attributedString, position: CGPoint(x: 255, y: 632)) self.charLayers = charPaths.map { path -> CAShapeLayer in let shapeLayer = CAShapeLayer() shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.strokeColor = UIColor.black.cgColor shapeLayer.lineWidth = lineWidth shapeLayer.path = path return shapeLayer } }

--------编辑

很抱歉忘记添加重要功能

见下文

drawText()

© www.soinside.com 2019 - 2024. All rights reserved.