{"id":35992,"date":"2022-05-25T14:15:54","date_gmt":"2022-05-25T18:15:54","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35992"},"modified":"2022-05-25T20:26:04","modified_gmt":"2022-05-26T00:26:04","slug":"attributedstring-attribute-scopes","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/05\/25\/attributedstring-attribute-scopes\/","title":{"rendered":"AttributedString Attribute Scopes"},"content":{"rendered":"<p><a href=\"https:\/\/nilcoalescing.com\/blog\/AttributedStringAttributeScopes\/\">Natalia Panferova<\/a>:<\/p>\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/AttributedStringAttributeScopes\/\"><p>UIKit and AppKit attribute scopes have many attributes with matching names but different value types. Attributes with types defined in UIKit such as <code>UIColor<\/code>, <code>UIFont<\/code> etc. are available on platforms that can import UIKit and attributes with types defined in AppKit such as <code>NSColor<\/code>, <code>NSFont<\/code> etc. are available on macOS. <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/attributescopes\/uikitattributes\">AttributeScopes.UIKitAttributes<\/a> type contains the full list of UIKit attributes and <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/attributescopes\/appkitattributes\">AttributeScopes.AppKitAttributes<\/a> contains the full list of AppKit attributes.<\/p><p>Foundation can infer the right type of the attribute to use when we assign the value. The following code will assign <code>UIColor.blue<\/code> as foreground color when UIKit is imported and <code>NSColor.blue<\/code> when AppKit is imported.<\/p><p>[&#8230;]<\/p><p>Note, that when SwiftUI is imported SwiftUI attributes take priority and the resulting attribute value will be <code>Color.blue<\/code> that is defined in the SwiftUI framework.<\/p><p>[&#8230;]<\/p><p>We can even define our own attributes and attribute scopes. This can be useful if we are building a framework or a package or just want to extend <code>AttributedString<\/code> functionality in our app.<\/p><\/blockquote>\n\n<p>See also: <a href=\"https:\/\/github.com\/ethanhuang13\/NSAttributedStringBuilder\">NSAttributedStringBuilder<\/a> (via <a href=\"https:\/\/twitter.com\/kthomas901\/status\/1149694365981278209\">Kaya Thomas<\/a>).<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/04\/27\/attributedstrings-codable-format\/\">AttributedString&rsquo;s Codable Format<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/04\/25\/heterogeneous-swift-dictionary-with-strong-types\/\">Heterogeneous Swift Dictionary With Strong Types<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/02\/16\/surprises-when-using-markdown-in-swiftui\/\">Surprises When Using Markdown in SwiftUI<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/01\/26\/swift-foundation-formatter-improvements\/\">Swift Foundation Formatter Improvements<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Natalia Panferova: UIKit and AppKit attribute scopes have many attributes with matching names but different value types. Attributes with types defined in UIKit such as UIColor, UIFont etc. are available on platforms that can import UIKit and attributes with types defined in AppKit such as NSColor, NSFont etc. are available on macOS. AttributeScopes.UIKitAttributes type contains [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-05-25T18:15:56Z","apple_news_api_id":"7e5db7f0-fb23-4eb6-bab6-35dc39a19c9b","apple_news_api_modified_at":"2022-05-26T00:26:06Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Afl238PsjTra6tjXcOaGcmw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,2078,30,2077,71,901,1812],"class_list":["post-35992","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=35992"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35992\/revisions"}],"predecessor-version":[{"id":35996,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35992\/revisions\/35996"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}