draw
的形状,最好使用attributed string
。有什么办法可以做到这一点?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()