{"id":10,"date":"2006-02-06T22:04:39","date_gmt":"2006-02-07T06:04:39","guid":{"rendered":"http:\/\/blog.danielparnell.com\/?p=10"},"modified":"2006-02-09T15:54:00","modified_gmt":"2006-02-09T23:54:00","slug":"accessing-the-string-and-attribute-data-in-a-nstextview","status":"publish","type":"post","link":"https:\/\/blog.danielparnell.com\/?p=10","title":{"rendered":"Accessing the string and attribute data in a NSTextView"},"content":{"rendered":"<p><span style=\"font-size: 12pt; font-family: Helvetica; \">NSTextView stores its data in a NSTextStorage object.  NSTextStorage descends from NSMutableAttributed string, so that gives us a clue.\u00c2\u00a0<br \/>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.\u00c2\u00a0<br \/>\u00c2\u00a0<br \/><\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSRange range;\u00c2\u00a0<br \/>\t\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">int<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \"> i;\u00c2\u00a0<br \/>\t\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">int<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \"> L = [<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">self<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \"> length];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0i = <\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #0000ff; \">0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">;\u00c2\u00a0<br \/>\t\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">while<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">(i&lt;L) {\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\t\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #236e25; \">\/\/ get all the attributes we are interested in<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSDictionary* attributes = [<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">self<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \"> attributesAtIndex: i effectiveRange: &#038;range];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSAttributedString* attPart = [<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #760f50; \">self<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \"> attributedSubstringFromRange: range];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSString* part = [attPart string];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSFont* font = [attributes objectForKey: NSFontAttributeName];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSColor* color = [attributes objectForKey: <\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #891315; \">@&#8221;NSColor&#8221;<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSParagraphStyle* paraStyle = [attributes objectForKey: NSParagraphStyleAttributeName];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSShadow* shadow = [attributes objectForKey: NSShadowAttributeName];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSURL* link = [attributes objectForKey: NSLinkAttributeName];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSTextAttachment* attachment = [attributes objectForKey: NSAttachmentAttributeName];\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\t\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0<\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; color: #236e25; \">\/\/ process your attributed string pieces here\u00c2\u00a0<br \/><\/span><span style=\"font-size: 10pt; font-family: Monaco, monospace; \">\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0i = range.location+range.length;\u00c2\u00a0<br \/>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0}\u00c2\u00a0<br \/><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NSTextView stores its data in a NSTextStorage object. NSTextStorage descends from NSMutableAttributed string, so that gives us a clue.\u00c2\u00a0The 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.\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0NSRange range;\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0int i;\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0int L = [self length];\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0i = 0;\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0while(i&lt;L) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.danielparnell.com\/?p=10\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Accessing the string and attribute data in a NSTextView&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,3,2],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-mac-os-x","category-programming","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p561S3-a","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":0,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.danielparnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}