{"id":1752,"date":"2013-08-16T10:45:02","date_gmt":"2013-08-16T06:45:02","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=1752"},"modified":"2013-08-16T10:45:02","modified_gmt":"2013-08-16T06:45:02","slug":"using-nsregularexpression-with-nsattributedstring","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=1752","title":{"rendered":"Using NSRegularExpression with NSAttributedString"},"content":{"rendered":"<p>I think, it is a very convenient way to use <strong>regular expressions<\/strong> to find a range for applying attributes to <strong>NSAttributedString<\/strong>. This is how I did it:<\/p>\n<pre>    NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:articleText];\r\n\r\n    NSRange range = [articleText rangeOfString:@\"\\\\[.+?\\\\]\" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];\r\n    if (range.location != NSNotFound) {\r\n        [goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@\"Georgia\" size:16] range:range];\r\n        [goodText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range];\r\n    }\r\n\r\n    NSString *regEx = [NSString stringWithFormat:@\"%@.+?\\\\s\", [self.article.titleText substringToIndex:0]];\r\n    range = [articleText rangeOfString:regEx options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];\r\n    if (range.location != NSNotFound) {\r\n        [goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@\"Georgia-Bold\" size:20] range:range];\r\n        [goodText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];\r\n    }\r\n\r\n    [self.textView setAttributedText:goodText];<\/pre>\n<p>I was searching for a list of available attributes and didn&#8217;t find them in a class reference&#8217;s first page. So I decided to post here information on that.<\/p>\n<p>Attributed strings support the following standard attributes for text.<\/p>\n<pre>    NSString *NSFontAttributeName;\r\n    NSString *NSParagraphStyleAttributeName;\r\n    NSString *NSForegroundColorAttributeName;\r\n    NSString *NSUnderlineStyleAttributeName;\r\n    NSString *NSSuperscriptAttributeName;\r\n    NSString *NSBackgroundColorAttributeName;\r\n    NSString *NSAttachmentAttributeName;\r\n    NSString *NSLigatureAttributeName;\r\n    NSString *NSBaselineOffsetAttributeName;\r\n    NSString *NSKernAttributeName;\r\n    NSString *NSLinkAttributeName;\r\n    NSString *NSStrokeWidthAttributeName;\r\n    NSString *NSStrokeColorAttributeName;\r\n    NSString *NSUnderlineColorAttributeName;\r\n    NSString *NSStrikethroughStyleAttributeName;\r\n    NSString *NSStrikethroughColorAttributeName;\r\n    NSString *NSShadowAttributeName;\r\n    NSString *NSObliquenessAttributeName;\r\n    NSString *NSExpansionAttributeName;\r\n    NSString *NSCursorAttributeName;\r\n    NSString *NSToolTipAttributeName;\r\n    NSString *NSMarkedClauseSegmentAttributeName;\r\n    NSString *NSWritingDirectionAttributeName;\r\n    NSString *NSVerticalGlyphFormAttributeName;\r\n    NSString *NSTextAlternativesAttributeName;<\/pre>\n<p><strong><a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSAttributedString_AppKitAdditions\/Reference\/Reference.html#\/\/apple_ref\/doc\/uid\/20000167\">Standart Attributes<\/a><\/strong><\/p>\n<p><strong><a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Conceptual\/AttributedStrings\/AttributedStrings.html#\/\/apple_ref\/doc\/uid\/10000036i\">NSAttributedString programming guide<\/a><\/strong><\/p>\n<p><strong><a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSAttributedString_Class\/Reference\/Reference.html\">A full class reference is here<\/a><\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think, it is a very convenient way to use regular expressions to find a range for applying attributes to NSAttributedString. This is how I did it: NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:articleText]; NSRange range = [articleText rangeOfString:@&#8221;\\\\[.+?\\\\]&#8221; options:NSRegularExpressionSearch|NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { [goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@&#8221;Georgia&#8221; size:16] range:range]; [goodText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range]; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1752","post","type-post","status-publish","format-standard","hentry","category-ios"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using NSRegularExpression with NSAttributedString - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=1752\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using NSRegularExpression with NSAttributedString - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"I think, it is a very convenient way to use regular expressions to find a range for applying attributes to NSAttributedString. This is how I did it: NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:articleText]; NSRange range = [articleText rangeOfString:@&quot;\\[.+?\\]&quot; options:NSRegularExpressionSearch|NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { [goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@&quot;Georgia&quot; size:16] range:range]; [goodText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range]; [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=1752\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-16T06:45:02+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Using NSRegularExpression with NSAttributedString\",\"datePublished\":\"2013-08-16T06:45:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752\"},\"wordCount\":82,\"commentCount\":0,\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752\",\"name\":\"Using NSRegularExpression with NSAttributedString - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2013-08-16T06:45:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=1752#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using NSRegularExpression with NSAttributedString\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using NSRegularExpression with NSAttributedString - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=1752","og_locale":"en_US","og_type":"article","og_title":"Using NSRegularExpression with NSAttributedString - Denis Kutlubaev","og_description":"I think, it is a very convenient way to use regular expressions to find a range for applying attributes to NSAttributedString. This is how I did it: NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:articleText]; NSRange range = [articleText rangeOfString:@\"\\[.+?\\]\" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { [goodText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@\"Georgia\" size:16] range:range]; [goodText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range]; [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=1752","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2013-08-16T06:45:02+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=1752#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=1752"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Using NSRegularExpression with NSAttributedString","datePublished":"2013-08-16T06:45:02+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=1752"},"wordCount":82,"commentCount":0,"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=1752#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=1752","url":"https:\/\/blog.alwawee.ru\/?p=1752","name":"Using NSRegularExpression with NSAttributedString - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2013-08-16T06:45:02+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=1752#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=1752"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=1752#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Using NSRegularExpression with NSAttributedString"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1752"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/1752\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}