[英]NSTextView with tokens
如何將標記(如NSTokenField
添加到NStextView
?
這實際上有點復雜。 您需要為每個“令牌”創建一個自定義NSTextAttachment
,並將其插入NSTextStorage
以用於您的NSTextView
。
Dejal Systems的David Sinclair發表了一篇很棒的文章 ,解釋了如何做到這一點。
我想出了一個簡單的方法,它使用自定義單元格類來標記:
NSTextAttachmentCell
並重新實現的單元類 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
NSTextView
令牌的類。 NSTextAttachment
的實例 將標記插入文本視圖的方法可能如下所示:
- (void)insertAttachmentCell:(NSTextAttachmentCell *)cell toTextView:(NSTextView *)textView
{
NSTextAttachment *attachment = [NSTextAttachment new];
[attachment setAttachmentCell:cell];
[textView insertText:[NSAttributedString attributedStringWithAttachment:attachment]];
}
這種方法比David Sinclair的方法更適合於令牌。 因為我們想要顯示動態內容(令牌)而不是靜態圖像,所以不需要使用文件包裝器。
看看大衛的概念可能會有用。 他描述了一種實現拖放操作的好方法。 復制粘貼功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.