-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathOEXTokenFieldCell.m
149 lines (116 loc) · 5.46 KB
/
OEXTokenFieldCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// OEXTokenFieldCell.m
// OEXTokenField
//
// Created by Nicolas BACHSCHMIDT on 16/03/2013.
// Copyright (c) 2013 Octiplex. All rights reserved.
//
#import "OEXTokenFieldCell.h"
#import "OEXTokenTextStorage.h"
#import <objc/runtime.h>
static int kOEXTokenFieldCellRepresentedObjectKey;
@interface OEXTokenFieldCell () <OEXTokenTextStorageDelegate>
@end
@implementation OEXTokenFieldCell
{
NSTokenFieldCell *_tokenCell;
NSArray *_objects;
}
@dynamic delegate;
#pragma mark - Attributed String Value
- (NSAttributedString *)attributedStringValue
{
// The token cells are drawn using text attachments
// Replace attachment cells before displaying the string
NSAttributedString *attrString = super.attributedStringValue;
NSRange range = NSMakeRange(0, attrString.length);
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) {
if ( attachment ) {
[self updateTokenAttachment:attachment forAttributedString:[attrString attributedSubstringFromRange:range]];
}
}];
return attrString;
}
- (void)setAttributedStringValue:(NSAttributedString *)attrString
{
// The default implementation of setAttributedString: cannot handle attachments with replaced cells
// Transform the attributed string to array of represented objects
NSMutableArray *objects = [NSMutableArray new];
NSRange range = NSMakeRange(0, attrString.length);
[attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(id attachment, NSRange range, BOOL *stop) {
id representedObject = [self representedObjectWithAttachment:attachment attributedString:[attrString attributedSubstringFromRange:range]];
[objects addObject:representedObject];
}];
[self setObjectValue:objects];
}
#pragma mark - Field Editor
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
// Replace the text storage of the text view so we can replace attachment cells on the fly
NSTextView *textView = (NSTextView *) [super setUpFieldEditorAttributes:textObj];
if ( [textView isKindOfClass:[NSTextView class]] )
{
NSLayoutManager *layoutManager = textView.textContainer.layoutManager;
OEXTokenTextStorage *textStorage = (OEXTokenTextStorage *) layoutManager.textStorage;
if ( ! [textStorage isKindOfClass:[OEXTokenTextStorage class]] ) {
textStorage = [[OEXTokenTextStorage alloc] initWithAttributedString:textStorage];
[layoutManager replaceTextStorage:textStorage];
}
textStorage.delegate = self;
}
return textView;
}
- (void)endEditing:(NSText *)textObj
{
NSTextView *textView = (NSTextView *) textObj;
if ( [textView isKindOfClass:[NSTextView class]] )
{
OEXTokenTextStorage *textStorage = (OEXTokenTextStorage *) textView.textContainer.layoutManager.textStorage;
if ( [textStorage isKindOfClass:[OEXTokenTextStorage class]] ) {
textStorage.delegate = nil;
}
}
[super endEditing:textObj];
}
#pragma mark - Token Replacement
- (void)updateTokenAttachment:(NSTextAttachment *)attachment forAttributedString:(NSAttributedString *)attrString
{
// If the represented object in set, we've already updated the attachment
if ( objc_getAssociatedObject(attachment, &kOEXTokenFieldCellRepresentedObjectKey) )
return;
id representedObject = [self representedObjectWithAttachment:attachment attributedString:attrString];
objc_setAssociatedObject(attachment, &kOEXTokenFieldCellRepresentedObjectKey, representedObject, OBJC_ASSOCIATION_RETAIN);
// Replace the attachment's cell
id <NSTextAttachmentCell> cell = attachment.attachmentCell;
cell = [self attachmentCellForRepresentedObject:representedObject] ?: cell;
[cell setAttachment:attachment];
[attachment setAttachmentCell:cell];
}
- (NSTextAttachmentCell *)attachmentCellForRepresentedObject:(id)representedObject;
{
NSTextAttachmentCell *cell = nil;
if ( [self.delegate respondsToSelector:@selector(tokenFieldCell:attachmentCellForRepresentedObject:)] ) {
cell = [self.delegate tokenFieldCell:self attachmentCellForRepresentedObject:representedObject];
}
cell.font = self.font;
return cell;
}
- (id)representedObjectWithAttachment:(NSTextAttachment *)attachment attributedString:(NSAttributedString *)attrString
{
// If the attachment was updated, we just need to access the associated object
if ( attachment && objc_getAssociatedObject(attachment, &kOEXTokenFieldCellRepresentedObjectKey) )
return objc_getAssociatedObject(attachment, &kOEXTokenFieldCellRepresentedObjectKey);
// This attributed string was generated by NSTokenField
// As we don't want to rely on private APIs (NSTokenAttachment/NSTokenAttachmentCell), let's just use a NSTokenFieldCell to do the job
if ( ! _tokenCell )
_tokenCell = [NSTokenFieldCell new];
_tokenCell.attributedStringValue = attrString;
NSArray *objectValue = _tokenCell.objectValue;
return objectValue.count ? objectValue[0] : attrString.string;
}
#pragma mark - OEXTokenTextStorageDelegate
- (void)tokenTextStorage:(OEXTokenTextStorage *)textStorage updateTokenAttachment:(NSTextAttachment *)attachment forRange:(NSRange)range
{
[self updateTokenAttachment:attachment forAttributedString:[textStorage attributedSubstringFromRange:range]];
}
@end