diff --git a/OEXTokenAttachmentCell.h b/OEXTokenAttachmentCell.h index 6f6bac5..6fac624 100644 --- a/OEXTokenAttachmentCell.h +++ b/OEXTokenAttachmentCell.h @@ -34,6 +34,9 @@ typedef NS_ENUM(NSUInteger, OEXTokenJoinStyle) { - (NSSize)cellSizeForTitleSize:(NSSize)titleSize; - (NSRect)titleRectForBounds:(NSRect)bounds; +- (CGFloat)titleMargin; +- (CGFloat)tokenMargin; + - (void)drawTokenWithFrame:(NSRect)rect inView:(NSView *)controlView; - (void)drawTitleWithFrame:(NSRect)rect inView:(NSView *)controlView; diff --git a/OEXTokenAttachmentCell.m b/OEXTokenAttachmentCell.m index 44e588e..7894983 100644 --- a/OEXTokenAttachmentCell.m +++ b/OEXTokenAttachmentCell.m @@ -18,6 +18,17 @@ @implementation OEXTokenAttachmentCell OEXTokenJoinStyle _joinStyle; } +- (CGFloat)titleMargin +{ + return kOEXTokenAttachmentTitleMargin; +} + + +- (CGFloat)tokenMargin +{ + return kOEXTokenAttachmentTokenMargin; +} + #pragma mark - Geometry - (NSPoint)cellBaselineOffset @@ -35,54 +46,60 @@ - (NSSize)cellSizeForTitleSize:(NSSize)titleSize { NSSize size = titleSize; // Add margins + height for the token rounded edges - size.width += size.height + kOEXTokenAttachmentTitleMargin * 2; + size.width += size.height + [self titleMargin] * 2; NSRect rect = {NSZeroPoint, size}; return NSIntegralRect(rect).size; } - (NSRect)titleRectForBounds:(NSRect)bounds { - bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTitleMargin * 2 + bounds.size.height); - return NSInsetRect(bounds, kOEXTokenAttachmentTitleMargin + bounds.size.height / 2, 0); + CGFloat titleMargin = [self titleMargin]; + bounds.size.width = MAX(bounds.size.width, titleMargin * 2 + bounds.size.height); + return NSInsetRect(bounds, titleMargin + bounds.size.height / 2, 0); } #pragma mark - Drawing - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView characterIndex:(NSUInteger)charIndex layoutManager:(NSLayoutManager *)layoutManager { - _drawingMode = self.isHighlighted && controlView.window.isKeyWindow ? OEXTokenDrawingModeHighlighted : OEXTokenDrawingModeDefault; - _joinStyle = OEXTokenJoinStyleNone; - - if ( [controlView respondsToSelector:@selector(selectedRanges)] ) + [self _drawWithFrame:cellFrame inView:controlView characterIndex:charIndex layoutManager:layoutManager]; +} + +- (void)_drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView characterIndex:(NSUInteger)charIndex layoutManager:(NSLayoutManager *)layoutManager +{ + _drawingMode = self.isHighlighted && controlView.window.isKeyWindow ? OEXTokenDrawingModeHighlighted : OEXTokenDrawingModeDefault; + _joinStyle = OEXTokenJoinStyleNone; + + if ( [controlView respondsToSelector:@selector(selectedRanges)] ) + { + for ( NSValue *rangeValue in [(id) controlView selectedRanges] ) { - for ( NSValue *rangeValue in [(id) controlView selectedRanges] ) - { - NSRange range = rangeValue.rangeValue; - if ( ! NSLocationInRange(charIndex, range) ) - continue; - - if ( controlView.window.isKeyWindow ) - _drawingMode = OEXTokenDrawingModeSelected; - - // TODO: RTL is not supported yet - if ( range.location < charIndex ) - _joinStyle |= OEXTokenJoinStyleLeft; - if ( NSMaxRange(range) > charIndex + 1 ) - _joinStyle |= OEXTokenJoinStyleRight; - } + NSRange range = rangeValue.rangeValue; + if ( ! NSLocationInRange(charIndex, range) ) + continue; + + if ( controlView.window.isKeyWindow ) + _drawingMode = OEXTokenDrawingModeSelected; + + // TODO: RTL is not supported yet + if ( range.location < charIndex ) + _joinStyle |= OEXTokenJoinStyleLeft; + if ( NSMaxRange(range) > charIndex + 1 ) + _joinStyle |= OEXTokenJoinStyleRight; } - - [self drawTokenWithFrame:cellFrame inView:controlView]; + } + + [self drawTokenWithFrame:cellFrame inView:controlView]; } - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - [self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil]; + [self _drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil]; } - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - [self drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil]; + [self _drawWithFrame:cellFrame inView:controlView characterIndex:NSNotFound layoutManager:nil]; } - (void)drawTokenWithFrame:(NSRect)rect inView:(NSView *)controlView @@ -173,10 +190,11 @@ - (NSColor *)tokenTitleColorForDrawingMode:(OEXTokenDrawingMode)drawingMode - (NSBezierPath *)tokenPathForBounds:(NSRect)bounds joinStyle:(OEXTokenJoinStyle)jointStyle { - bounds.size.width = MAX(bounds.size.width, kOEXTokenAttachmentTokenMargin * 2 + bounds.size.height); + CGFloat tokenMargin = [self tokenMargin]; + bounds.size.width = MAX(bounds.size.width, tokenMargin * 2 + bounds.size.height); CGFloat radius = bounds.size.height / 2; - CGRect innerRect = NSInsetRect(bounds, kOEXTokenAttachmentTokenMargin + radius, 0); + CGRect innerRect = NSInsetRect(bounds, tokenMargin + radius, 0); CGFloat x0 = NSMinX(bounds); CGFloat x1 = NSMinX(innerRect); CGFloat x2 = NSMaxX(innerRect); diff --git a/OEXTokenField.h b/OEXTokenField.h index a19e20e..d264f91 100644 --- a/OEXTokenField.h +++ b/OEXTokenField.h @@ -21,7 +21,7 @@ /** The token field's delegate. @discussion The delegate must adopt the `` protocol. */ -@property(nonatomic, assign) id delegate; +@property (atomic, weak) id delegate; @end diff --git a/OEXTokenFieldCell.h b/OEXTokenFieldCell.h index ffce10d..7ac2d7c 100644 --- a/OEXTokenFieldCell.h +++ b/OEXTokenFieldCell.h @@ -19,7 +19,7 @@ /** The token field cell's delegate. @discussion The delegate must adopt the `` protocol. */ -@property(nonatomic, assign) id delegate; +@property(weak) id delegate; /** @name Displaying Tokenized Attachment Cells */ diff --git a/OEXTokenFieldCell.m b/OEXTokenFieldCell.m index 5ab1f96..e9d007c 100644 --- a/OEXTokenFieldCell.m +++ b/OEXTokenFieldCell.m @@ -13,7 +13,7 @@ static int kOEXTokenFieldCellRepresentedObjectKey; -@interface OEXTokenFieldCell () +@interface OEXTokenFieldCell () @end @implementation OEXTokenFieldCell @@ -33,9 +33,9 @@ - (NSAttributedString *)attributedStringValue NSAttributedString *attrString = super.attributedStringValue; NSRange range = NSMakeRange(0, attrString.length); - [attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(NSTextAttachment *attachment, NSRange range, BOOL *stop) { + [attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(NSTextAttachment *attachment, NSRange arange, BOOL *stop) { if ( attachment ) { - [self updateTokenAttachment:attachment forAttributedString:[attrString attributedSubstringFromRange:range]]; + [self updateTokenAttachment:attachment forAttributedString:[attrString attributedSubstringFromRange:arange]]; } }]; return attrString; @@ -48,8 +48,8 @@ - (void)setAttributedStringValue:(NSAttributedString *)attrString 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]]; + [attrString enumerateAttribute:NSAttachmentAttributeName inRange:range options:0 usingBlock:^(id attachment, NSRange arange, BOOL *stop) { + id representedObject = [self representedObjectWithAttachment:attachment attributedString:[attrString attributedSubstringFromRange:arange]]; [objects addObject:representedObject]; }]; diff --git a/OEXTokenTextStorage.h b/OEXTokenTextStorage.h index 463a8ab..d798716 100644 --- a/OEXTokenTextStorage.h +++ b/OEXTokenTextStorage.h @@ -14,7 +14,7 @@ */ @interface OEXTokenTextStorage : NSTextStorage -@property(nonatomic, weak) id delegate; +@property(weak) id delegate; - (id)initWithAttributedString:(NSAttributedString *)attrStr; @@ -27,4 +27,4 @@ @optional - (void)tokenTextStorage:(OEXTokenTextStorage *)textStorage updateTokenAttachment:(NSTextAttachment *)attachment forRange:(NSRange)range; -@end \ No newline at end of file +@end diff --git a/OEXTokenTextStorage.m b/OEXTokenTextStorage.m index ad8bd71..a7bed3d 100644 --- a/OEXTokenTextStorage.m +++ b/OEXTokenTextStorage.m @@ -13,6 +13,8 @@ @implementation OEXTokenTextStorage NSMutableAttributedString *_string; } +@dynamic delegate; + #pragma mark - init - (id)initWithAttributedString:(NSAttributedString *)attrStr @@ -51,8 +53,8 @@ - (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]; + if ( attachment && [self.delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] ) + [self.delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:range]; [self edited:NSTextStorageEditedAttributes range:range changeInLength:0]; } @@ -69,9 +71,9 @@ - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttribut [_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]; + [_string enumerateAttribute:NSAttachmentAttributeName inRange:strRange options:0 usingBlock:^(NSTextAttachment *attachment, NSRange arange, BOOL *stop) { + if ( attachment && [self.delegate respondsToSelector:@selector(tokenTextStorage:updateTokenAttachment:forRange:)] ) { + [self.delegate tokenTextStorage:self updateTokenAttachment:attachment forRange:arange]; } }]; [self edited:NSTextStorageEditedAttributes | NSTextStorageEditedCharacters range:range changeInLength:strRange.length - range.length];