TextAnnotation 0.6.2

TextAnnotation 0.6.2

Maintained by Julian Drapaylo, Mirko Kiefer, Lucas Dworakowski.



  • By
  • Mirko Kiefer

TextAnnotation

CI Status Version License Platform

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:

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

TextAnnotation is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'TextAnnotation'

Usage

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.

Example

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)
  }
}

Interface Definition

TextAnnotation

public protocol TextAnnotation {
  var text: String { get set }
  var frame: CGRect { get set }
  var isSelected: Bool { get set }
  var isEditing: Bool { get set }
}

TextAnnotationCanvas

public protocol TextAnnotationCanvas {
  var view: NSView { get }
  
  func addTextAnnotation(_ textAnnotation: TextAnnotation)
  func textAnnotationCanvasMouseDown(event: NSEvent)
}

TextAnnotationDelegate

public protocol TextAnnotationDelegate {
  func textAnnotationDidEdit(_ textAnnotation: TextAnnotation)
  func textAnnotationDidMove(_ textAnnotation: TextAnnotation)
}

Author

Mirko Kiefer, [email protected]

License

TextAnnotation is available under the MIT license. See the LICENSE file for more info.