objc2_foundation/
attributed_string.rs

1use core::panic::{RefUnwindSafe, UnwindSafe};
2
3use objc2::rc::Retained;
4use objc2::AnyThread;
5
6use crate::*;
7
8// Same reasoning as `NSString`.
9impl UnwindSafe for NSAttributedString {}
10impl RefUnwindSafe for NSAttributedString {}
11
12impl NSAttributedString {
13    /// Creates a new attributed string from the given string and attributes.
14    ///
15    /// The attributes are associated with every UTF-16 code unit in the
16    /// string.
17    ///
18    /// # Safety
19    ///
20    /// The attributes must be valid.
21    #[doc(alias = "initWithString:")]
22    #[cfg(feature = "NSDictionary")]
23    #[cfg(feature = "NSString")]
24    pub unsafe fn new_with_attributes(
25        string: &NSString,
26        attributes: &NSDictionary<NSAttributedStringKey, objc2::runtime::AnyObject>,
27    ) -> Retained<Self> {
28        unsafe { Self::initWithString_attributes(Self::alloc(), string, Some(attributes)) }
29    }
30
31    /// Creates a new attributed string without any attributes.
32    #[doc(alias = "initWithString:")]
33    #[cfg(feature = "NSString")]
34    pub fn from_nsstring(string: &NSString) -> Retained<Self> {
35        Self::initWithString(Self::alloc(), string)
36    }
37}
38
39impl NSMutableAttributedString {
40    // TODO: new_with_attributes
41
42    #[doc(alias = "initWithString:")]
43    #[cfg(feature = "NSString")]
44    pub fn from_nsstring(string: &NSString) -> Retained<Self> {
45        Self::initWithString(Self::alloc(), string)
46    }
47
48    #[doc(alias = "initWithAttributedString:")]
49    pub fn from_attributed_nsstring(attributed_string: &NSAttributedString) -> Retained<Self> {
50        Self::initWithAttributedString(Self::alloc(), attributed_string)
51    }
52}