One Quickie


Iterating attributes in an attributed string (NSString->General)
This prints out each of the attribute runs from an attributed string:
NSAttributedString *string = ...;

NSRange totalRange = NSMakeRange (0, string.length);
[string enumerateAttributesInRange: totalRange
        options: 0
        usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

            NSLog (@"range: %@  attributes: %@",
                   NSStringFromRange(range), attributes);
    }];


and if you're in 10.5 or earlier:
- (void) iterateAttributesForString: (NSAttributedString *) string
{
    NSDictionary *attributeDict;
    NSRange effectiveRange = { 0, 0 };

    do {
        NSRange range;
        range = NSMakeRange (NSMaxRange(effectiveRange),
                             [string length] - NSMaxRange(effectiveRange));

        attributeDict = [string attributesAtIndex: range.location
                                longestEffectiveRange: &effectiveRange
                                inRange: range];

        NSLog (@"Range: %@  Attributes: %@",
               NSStringFromRange(effectiveRange), attributeDict);

    } while (NSMaxRange(effectiveRange) < [string length]);

} // iterateAttributesForString



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com