This code snippet is a category of NSAttributedString that shows how to fetch all attachments from the string.
Header:
@interface NSAttributedString (NSAttributedString_MoreExtensions)
/**
* @method allAttachments
* @abstract Fetchs all attachments from an NSAttributedString.
* @discussion This method searchs for NSAttachmentAttributeName attributes within the string instead of searching for NSAttachmentCharacter characters.
*/
- (NSArray *)allAttachments;
@end
Source:
@implementation NSAttributedString (NSAttributedString_MoreExtensions)
- (NSArray *)allAttachments
{
NSMutableArray *theAttachments = [NSMutableArray array];
NSRange theStringRange = NSMakeRange(0, [self length]);
if (theStringRange.length > 0)
{
unsigned N = 0;
do
{
NSRange theEffectiveRange;
NSDictionary *theAttributes = [self attributesAtIndex:N longestEffectiveRange:&theEffectiveRange inRange:theStringRange];
NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
if (theAttachment != NULL)
[theAttachments addObject:theAttachment];
N = theEffectiveRange.location + theEffectiveRange.length;
}
while (N < theStringRange.length);
}
return(theAttachments);
}
@end