// // DSAttributedStringCategories.h // Shared categories // // Created by David Sinclair on Fri Jul 18 2003. // Copyright © 2003 - 2007 Dejal Systems, LLC. All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // Redistributions of source code must retain this list of conditions and the following disclaimer. // // The name of Dejal Systems, LLC may not be used to endorse or promote products derived from this // software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THIS SOFTWARE. // #import "DSAttributedStringCategories.h" @implementation NSAttributedString (DSAttributedStringCategories) /* attributedString Convenience method to create and return an empty, autoreleased attributed string. Written by DJS 2003-07. */ + (id)attributedString { return [[[self alloc] initWithString:@""] autorelease]; } /* attributedStringWithString: Convenience method to create and return an autoreleased attributed string from the specified string. Written by DJS 2003-07. */ + (id)attributedStringWithString:(NSString *)string { return [[[self alloc] initWithString:string] autorelease]; } /* attributedStringWithString:attributes: Convenience method to create and return an autoreleased attributed string from the specified string and attributes. Written by DJS 2004-10. */ + (id)attributedStringWithString:(NSString *)string attributes:(NSDictionary *)attributes { return [[[self alloc] initWithString:string attributes:attributes] autorelease]; } /* attributedStringWithImageFromPath: Convenience method to create and return an autoreleased attributed string containing an image from the specified path. Returns nil if the path is nil or there is no image in that location. Written by DJS 2006-06. */ + (id)attributedStringWithImageFromPath:(NSString *)path; { if (!path) return nil; NSFileWrapper *fileWrapper = [[[NSFileWrapper alloc] initWithPath:path] autorelease]; if (!fileWrapper) return nil; NSTextAttachment *attachment = [[[NSTextAttachment alloc] initWithFileWrapper:fileWrapper] autorelease]; if (!attachment) return nil; return [self attributedStringWithAttachment:attachment]; } /* attributedStringWithRTF: Convenience method to create and return an autoreleased attributed string from the specified RTF data and document attributes (which can be nil). Written by DJS 2003-07. */ + (id)attributedStringWithRTF:(NSData *)data { return [[[self alloc] initWithRTF:data documentAttributes:nil] autorelease]; } /* attributedStringWithRTFD: Convenience method to create and return an autoreleased attributed string from the specified RTFD data and document attributes (which can be nil). Written by DJS 2003-07. */ + (id)attributedStringWithRTFD:(NSData *)data { return [[[self alloc] initWithRTFD:data documentAttributes:nil] autorelease]; } // ---------------------------------------------------------------------------------------- #pragma mark - // ---------------------------------------------------------------------------------------- /* allRange Returns a range enclosing the entire attributed string. Written by DJS 2004-12. */ - (NSRange)allRange { return NSMakeRange(0, [self length]); } /* RTFValue Returns a RTF edition of the attributed string. Written by DJS 2004-12. */ - (NSData *)RTFValue { return [self RTFFromRange:[self allRange] documentAttributes:nil]; } /* RTFDValue Returns a RTFD edition of the attributed string. Written by DJS 2004-12. */ - (NSData *)RTFDValue { return [self RTFDFromRange:[self allRange] documentAttributes:nil]; } @end // ---------------------------------------------------------------------------------------- #pragma mark - // ---------------------------------------------------------------------------------------- @implementation NSMutableAttributedString (DSMutableAttributedStringCategories) /* appendString:withAttributes: Appends the string to the receiver, using the specified attributes. Written by DJS 2007-02. */ - (void)appendString:(NSString *)string withAttributes:(NSDictionary *)attributes; { [self appendAttributedString:[[self class] attributedStringWithString:string attributes:attributes]]; } /* insertString:withAttributes:atIndex: Inserts the string into the receiver at the index position, using the specified attributes. Written by DJS 2007-02. */ - (void)insertString:(NSString *)string withAttributes:(NSDictionary *)attributes atIndex:(int)index; { [self insertAttributedString:[[self class] attributedStringWithString:string attributes:attributes] atIndex:index]; } @end