Accessing the string and attribute data in a NSTextView

NSTextView stores its data in a NSTextStorage object. NSTextStorage descends from NSMutableAttributed string, so that gives us a clue. 
The easiest thing to do is to make a category on the NSAttributedString class to do whatever it is you want containing something like the following. 
 
    NSRange range; 
    
int i; 
    
int L = [self length]; 
             
    i =
0; 
    
while(i<L) { 
         
// get all the attributes we are interested in 
        NSDictionary* attributes = [
self attributesAtIndex: i effectiveRange: &range]; 
        NSAttributedString* attPart = [
self attributedSubstringFromRange: range]; 
        NSString* part = [attPart string]; 
        NSFont* font = [attributes objectForKey: NSFontAttributeName]; 
        NSColor* color = [attributes objectForKey:
@”NSColor”]; 
        NSParagraphStyle* paraStyle = [attributes objectForKey: NSParagraphStyleAttributeName]; 
        NSShadow* shadow = [attributes objectForKey: NSShadowAttributeName]; 
        NSURL* link = [attributes objectForKey: NSLinkAttributeName]; 
         
        NSTextAttachment* attachment = [attributes objectForKey: NSAttachmentAttributeName]; 
         
         
// process your attributed string pieces here 
 
        i = range.location+range.length; 
    } 

Join the Conversation

5 Comments

  1. Pingback: Jack Arnolds Hub
Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.