A textbox component that behaves like typical drawing or annotation apps require - think Sketch, Skitch, CloudApp etc.
The module is designed to be used alongside other drawing modules which for example support pen, rectangle or arrow annotations.
This module is a work in progress - to contribute review the spec:
To run the example project, clone the repo, and run pod install
from the Example directory first.
TextAnnotation is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'TextAnnotation'
Text annotation support can be added by adopting the TextAnnotationCanvas
protocol.
The protocol adds default handling of click and drag events but needs to be notified by those.
The TextAnnotationDelegate
protocol can be used to handle editing and move operations.
Newly created TextAnnotation
instances get by default the controller assigned as delegate.
class ViewController: NSViewController, TextAnnotationCanvas {
override func viewDidLoad() {
super.viewDidLoad()
// Programmatically creating a text annotation
let location = CGPoint(x: 100, y: 150)
// Method supplied by the TextAnnotationCanvas protocol extension
let textAnnotation = createTextAnnotation(text: "Some text", location: location)
textAnnotation.addTo(canvas: self)
}
override func mouseDown(with event: NSEvent) {
// TextAnnotationCanvas needs to handle mouse down events
let didHandle = textAnnotationCanvasMouseDown(event: event)
}
}
extension ViewController: TextAnnotationDelegate {
func textAnnotationDidEdit(textAnnotation: TextAnnotation) {
print(textAnnotation.text)
}
func textAnnotationDidMove(textAnnotation: TextAnnotation) {
print(textAnnotation.frame)
}
}
public protocol TextAnnotation {
var text: String { get set }
var frame: CGRect { get set }
var isSelected: Bool { get set }
var isEditing: Bool { get set }
}
public protocol TextAnnotationCanvas {
var view: NSView { get }
func addTextAnnotation(_ textAnnotation: TextAnnotation)
func textAnnotationCanvasMouseDown(event: NSEvent)
}
public protocol TextAnnotationDelegate {
func textAnnotationDidEdit(_ textAnnotation: TextAnnotation)
func textAnnotationDidMove(_ textAnnotation: TextAnnotation)
}
Mirko Kiefer, [email protected]
TextAnnotation is available under the MIT license. See the LICENSE file for more info.