Hey guys,
I keep getting these syntax errors when trying to build my code.
The errors:
Cannot find 'uiImage' in scope
Conditional downcast to CoreFoundation type 'CGFont' will always succeed
Value of type 'CGFont' has no member 'nameToGlyph'
The code in question:
import SwiftUI
struct DrawingView: UIViewRepresentable {
let template: String
func makeUIView(context: Context) -> DrawingCanvas {
let view = DrawingCanvas()
view.loadTemplate(template)
return view
}
func updateUIView(_ uiView: DrawingCanvas, context: Context) {}
typealias UIViewType = DrawingCanvas
}
class DrawingCanvas: UIView {
private let shapeLayer = CAShapeLayer()
private var points = [CGPoint]()
private var paths = [CGPath]()
private var templatePath: CGPath?
var isDrawingEnabled = false
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
private func setup() {
backgroundColor = .white
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.lineWidth = 5
shapeLayer.lineCap = .round
shapeLayer.lineJoin = .round
layer.addSublayer(shapeLayer)
}
func loadTemplate(_ template: String) {
let font = UIFont.systemFont(ofSize: bounds.size.height)
guard let imageRef = uiImage.cgImage else {
return
}
if let cgFont = font.fontDescriptor.fontAttributes[UIFontDescriptor.AttributeName(rawValue: kCTFontURLAttribute as String)] as? CGFont,
let cgGlyph = cgFont.nameToGlyph[template] {
if let glyphPath = CTFontCreatePathForGlyph(font, cgGlyph, nil) {
templatePath = glyphPath
shapeLayer.path = templatePath
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isDrawingEnabled else { return }
let touch = touches.first!
let location = touch.location(in: self)
points = [location]
shapeLayer.path = nil
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isDrawingEnabled else { return }
let touch = touches.first!
let location = touch.location(in: self)
points.append(location)
updateShapeLayer()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard isDrawingEnabled else { return }
paths.append(shapeLayer.path!)
shapeLayer.path = templatePath
points.removeAll()
NotificationCenter.default.post(name: DrawingView.drawingCompleteNotification, object: nil)
}
private func updateShapeLayer() {
let path = CGMutablePath()
path.addLines(between: points)
shapeLayer.path = path
}
func clear() {
paths.removeAll()
shapeLayer.path = templatePath
}
static let drawingCompleteNotification = Notification.Name("DrawingCompleteNotification")
}
The specific function:
func loadTemplate(_ template: String) {
let font = UIFont.systemFont(ofSize: bounds.size.height)
guard let imageRef = uiImage.cgImage else {
return
}
if let cgFont = font.fontDescriptor.fontAttributes[UIFontDescriptor.AttributeName(rawValue: kCTFontURLAttribute as String)] as? CGFont,
let cgGlyph = cgFont.nameToGlyph[template] {
if let glyphPath = CTFontCreatePathForGlyph(font, cgGlyph, nil) {
templatePath = glyphPath
shapeLayer.path = templatePath
}
}
}
Any help would be nice, thanks!