-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathOEXTokenTextStorage.m
80 lines (64 loc) · 2.52 KB
/
OEXTokenTextStorage.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
//
// OEXTokenTextStorage.m
// OEXTokenField
//
// Created by Nicolas BACHSCHMIDT on 16/03/2013.
// Copyright (c) 2013 Octiplex. All rights reserved.
//
#import "OEXTokenTextStorage.h"
@implementation OEXTokenTextStorage
{
NSMutableAttributedString *_string;
}
#pragma mark - init
- (id)initWithAttributedString:(NSAttributedString *)attrStr
{
if ( ! (self = [super init]) )
return nil;
_string = [[NSMutableAttributedString alloc] initWithAttributedString:attrStr];
return self;
}
- (id)init
{
return [self initWithAttributedString:nil];
}
#pragma mark - Primitive Methods
- (NSString *)string
{
return [_string string];
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
return [_string attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
[_string replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters range:range changeInLength:str.length - range.length];
}
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
{
[_string setAttributes:attrs range:range];
NSTextAttachment *attachment = attrs[NSAttachmentAttributeName];
if ( attachment && [_delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] )
[_delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
#pragma mark - Convenience Methods
- (void)removeAttribute:(NSString *)name range:(NSRange)range
{
[_string removeAttribute:name range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString
{
[_string replaceCharactersInRange:range withAttributedString:attrString];
NSRange strRange = NSMakeRange(range.location, attrString.length);
[_string enumerateAttribute:NSAttachmentAttributeName inRange:strRange options:0 usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) {
if ( attachment && [_delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] ) {
[_delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range];
}
}];
[self edited:NSTextStorageEditedAttributes | NSTextStorageEditedCharacters range:range changeInLength:strRange.length - range.length];
}
@end