rust_macios/foundation/
ns_attributed_string.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    object,
5    objective_c_runtime::{
6        id,
7        macros::interface_impl,
8        nil,
9        traits::{FromId, PNSObject},
10    },
11    utils::to_bool,
12};
13
14use super::{
15    NSAttributedStringDocumentAttributeKey, NSAttributedStringDocumentReadingOptionKey,
16    NSAttributedStringKey, NSData, NSDictionary, NSError, NSRange, NSRangePointer, NSString, UInt,
17};
18
19object! {
20    /// A string with associated attributes (such as visual style, hyperlinks,
21    /// or accessibility data) for portions of its text.
22    unsafe pub struct NSAttributedString;
23}
24
25#[interface_impl(NSObject)]
26impl NSAttributedString {
27    /* Creating an Attributed String
28     */
29
30    /// Creates an attributed string with the characters of the specified string and no attribute information.
31    #[method]
32    pub fn init_with_string(string: &NSString) -> Self
33    where
34        Self: Sized + FromId,
35    {
36        unsafe { Self::from_id(msg_send![Self::m_class(), initWithString: string.m_self()]) }
37    }
38
39    /// Creates an attributed string with the specified string and attributes.
40    #[method]
41    pub fn init_with_string_attributes(
42        string: &NSString,
43        attributes: &NSDictionary<NSAttributedStringKey, id>,
44    ) -> Self
45    where
46        Self: Sized + FromId,
47    {
48        unsafe {
49            Self::from_id(
50                msg_send![Self::m_class(), initWithString: string.m_self() attributes: attributes.m_self()],
51            )
52        }
53    }
54
55    /// Creates an attributed string with the characters and attributes of the specified attributed string.
56    #[method]
57    pub fn init_with_attributed_string(attr_string: &NSAttributedString) -> Self
58    where
59        Self: Sized + FromId,
60    {
61        unsafe {
62            Self::from_id(msg_send![
63                Self::m_class(),
64                initWithAttributedString: attr_string.m_self()
65            ])
66        }
67    }
68
69    /// Creates an attributed string from the data in the specified data object.
70    #[method]
71    pub fn init_with_data_options_document_attributes(
72        data: &NSData,
73        options: &NSDictionary<NSAttributedStringDocumentReadingOptionKey, id>,
74        document: &NSDictionary<NSAttributedStringDocumentAttributeKey, id>,
75    ) -> Result<Self, NSError>
76    where
77        Self: Sized + FromId,
78    {
79        unsafe {
80            let mut error = NSError::m_alloc();
81            let result = Self::from_id(
82                msg_send![Self::m_class(), initWithData: data.m_self() options: options.m_self() documentAttributes: document.m_self() error: &mut error],
83            );
84
85            if error.m_self() == nil {
86                Ok(result)
87            } else {
88                Err(error)
89            }
90        }
91    }
92
93    /* Retrieving Character Information
94     */
95
96    /// The character contents of the attributed string as a string.
97    #[property]
98    pub fn string(&self) -> NSString {
99        unsafe { NSString::from_id(msg_send![self.m_self(), string]) }
100    }
101
102    /// The length of the attributed string.
103    #[property]
104    pub fn length(&self) -> UInt {
105        unsafe { msg_send![self.m_self(), length] }
106    }
107
108    /* Retrieving Attribute Information
109     */
110
111    /// Returns the attributes for the character at the specified index.
112    #[method]
113    pub fn attributes_at_index_effective_range(
114        &self,
115        location: UInt,
116        range: NSRangePointer,
117    ) -> NSDictionary<NSAttributedStringKey, id> {
118        unsafe {
119            NSDictionary::from_id(
120                msg_send![self.m_self(), attributesAtIndex: location effectiveRange: range],
121            )
122        }
123    }
124
125    /// Returns the attributes for the character at the specified index and, by reference, the range where the attributes apply.
126    #[method]
127    pub fn attributes_at_index_longest_effective_range_in_range(
128        &self,
129        location: UInt,
130        range: NSRangePointer,
131        range_limit: NSRange,
132    ) -> NSDictionary<NSAttributedStringKey, id> {
133        unsafe {
134            NSDictionary::from_id(
135                msg_send![self.m_self(), attributesAtIndex: location longestEffectiveRange: range inRange: range_limit],
136            )
137        }
138    }
139
140    /* Comparing Attributed Strings
141     */
142
143    /// Returns a Boolean value that indicates whether the attributed string is equal to another attributed string.
144    #[method]
145    pub fn is_equal_to_attributed_string(&self, other: &NSAttributedString) -> bool {
146        unsafe { to_bool(msg_send![self.m_self(), isEqualToAttributedString: other.m_self()]) }
147    }
148
149    /* Extracting a Substring
150     */
151
152    /// Returns an attributed string consisting of the characters and attributes within the specified range in the attributed string.
153    #[method]
154    pub fn attributed_substring_from_range(&self, range: NSRange) -> NSAttributedString {
155        unsafe {
156            NSAttributedString::from_id(msg_send![
157                self.m_self(),
158                attributedSubstringFromRange: range
159            ])
160        }
161    }
162}