TextAnnotation
这是一个具有像典型绘图或注释应用(如Sketch、Skitch、CloudApp等)行为的文本框组件。
该模块旨在与支持笔、矩形或箭头注释等绘图模块一起使用。
此模块仍在开发中 - 要贡献力量,请审查规范
示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行 pod install
需求
安装
TextAnnotation可通过CocoaPods获取。要安装,只需将以下行添加到Podfile中:
pod 'TextAnnotation'
使用
通过采用 TextAnnotationCanvas
协议可以添加文本注释支持。该协议增加默认的处理点击和拖拽事件,但需要通知这些事件。可以使用 TextAnnotationDelegate
协议来处理编辑和移动操作。新创建的 TextAnnotation
实例默认由控制器分配为代理。
示例
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)
}
}
接口定义
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)
}
作者
米尔科·基弗,[email protected]
许可证
TextAnnotation可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。