{"id":428,"date":"2023-03-04T18:36:27","date_gmt":"2023-03-04T18:36:27","guid":{"rendered":"https:\/\/ootips.org\/yonat\/?p=428"},"modified":"2023-03-04T18:36:27","modified_gmt":"2023-03-04T18:36:27","slug":"find-the-writing-direction-of-a-string","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/find-the-writing-direction-of-a-string\/","title":{"rendered":"Find The Writing Direction Of A String"},"content":{"rendered":"\n<p>When you need to know if a string is right-to-left (rtl) or left-to-right (ltr) &#8211; don&#8217;t check individual characters. Instead, use the Natural Language framework, available since iOS 12 and macOS 10.14:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: swift; title: ; notranslate\" title=\"\">\nimport NaturalLanguage\n\nextension String {\n    var isRightToLeft: Bool {\n        guard let language = NLLanguageRecognizer.dominantLanguage(for: self) else { return false }\n        switch language {\n        case .arabic, .hebrew, .persian, .urdu:\n            return true\n        default:\n            return false\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>Then just use it like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: swift; title: ; notranslate\" title=\"\">\nif &quot;\u05e9\u05dc\u05d5\u05dd and \u0633\u0644\u0627\u0645&quot;.isRightToLeft {\n    \/\/ do what needs to be done\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>When you need to know if a string is right-to-left (rtl) or left-to-right (ltr) &#8211; don&#8217;t check individual characters. Instead, use the Natural Language framework, available since iOS 12 and macOS 10.14: Then just use it like this:<\/p>\n","protected":false},"author":1,"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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[7,14,17],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ios","tag-sample-code","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-6U","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":8,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/428\/revisions\/438"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}