Skip to main content

polyhorn_ios_sys/foundation/
attributed_string.rs

1use objc::runtime::*;
2use objc::*;
3
4use super::{NSParagraphStyle, NSString};
5use crate::coregraphics::{CGRect, CGSize};
6use crate::uikit::{UIColor, UIFont};
7use crate::Raw;
8
9/// Attributes that you can apply to text in an attributed string.
10pub struct NSAttributes {
11    /// The font of the text. The value of this attribute is a `UIFont` object.
12    /// Use this attribute to change the font for a range of text. If you do not
13    /// specify this attribute, the string uses a 12-point Helvetica (Neue) font
14    /// by default.
15    pub font: UIFont,
16
17    /// The color of the text. The value of this attribute is a `UIColor`
18    /// object. Use this attribute to specify the color of the text during
19    /// rendering. If you do not specify this attribute, the text is rendered in
20    /// black.
21    pub foreground_color: UIColor,
22
23    /// The paragraph style of the text. The value of this attribute is an
24    /// `NSParagraphStyle` object. Use this attribute to apply multiple
25    /// attributes to a range of text. If you do not specify this attribute, the
26    /// string uses the default paragraph attributes, as returned by the
27    /// `defaultParagraphStyle` of `NSParagraphStyle`.
28    pub paragraph_style: NSParagraphStyle,
29}
30
31extern "C" {
32    /// The font of the text. The value of this attribute is a `UIFont` object.
33    /// Use this attribute to change the font for a range of text. If you do not
34    /// specify this attribute, the string uses a 12-point Helvetica (Neue) font
35    /// by default.
36    static NSFontAttributeName: *mut Object;
37
38    /// The color of the text. The value of this attribute is a `UIColor`
39    /// object. Use this attribute to specify the color of the text during
40    /// rendering. If you do not specify this attribute, the text is rendered in
41    /// black.
42    static NSForegroundColorAttributeName: *mut Object;
43
44    /// The paragraph style of the text. The value of this attribute is an
45    /// `NSParagraphStyle` object. Use this attribute to apply multiple
46    /// attributes to a range of text. If you do not specify this attribute, the
47    /// string uses the default paragraph attributes, as returned by the
48    /// `defaultParagraphStyle` of `NSParagraphStyle`.
49    static NSParagraphStyleAttributeName: *mut Object;
50}
51
52impl NSAttributes {
53    /// Converts a structure of attributes to a `NSDictionary`.
54    pub fn into_dictionary(self) -> *mut Object {
55        unsafe {
56            let mut dictionary: *mut Object = msg_send![class!(NSMutableDictionary), alloc];
57            dictionary = msg_send![dictionary, init];
58            let _: () = msg_send![dictionary, setObject: self.font.as_raw()
59                                                 forKey: NSFontAttributeName];
60            let _: () = msg_send![dictionary, setObject: self.foreground_color.as_raw()
61                                                 forKey: NSForegroundColorAttributeName];
62            let _: () = msg_send![dictionary, setObject: self.paragraph_style.as_raw()
63                                                 forKey: NSParagraphStyleAttributeName];
64            dictionary
65        }
66    }
67}
68
69/// A string with associated attributes (such as visual style, hyperlinks, or
70/// accessibility data) for portions of its text.
71pub struct NSAttributedString {
72    object: *mut Object,
73}
74
75/// A mutable string object that also contains attributes (such as visual style,
76/// hyperlinks, or accessibility data) associated with various portions of its
77/// text content.
78pub struct NSMutableAttributedString {
79    object: *mut Object,
80}
81
82impl NSMutableAttributedString {
83    /// Initializes a newly allocated mutable attributed string.
84    pub fn new() -> NSMutableAttributedString {
85        unsafe {
86            let mut object: *mut Object = msg_send![class!(NSMutableAttributedString), alloc];
87            object = msg_send![object, init];
88            NSMutableAttributedString { object }
89        }
90    }
91
92    /// Adds the characters and attributes of a given attributed string to the
93    /// end of the receiver.
94    pub fn append_attributed_string(&mut self, string: &NSAttributedString) {
95        unsafe {
96            let _: () = msg_send![self.object, appendAttributedString: string.as_raw()];
97        }
98    }
99}
100
101impl Raw for NSMutableAttributedString {
102    unsafe fn from_raw(object: *mut Object) -> Self {
103        NSMutableAttributedString { object }
104    }
105
106    unsafe fn as_raw(&self) -> *mut Object {
107        self.object
108    }
109}
110
111impl Drop for NSMutableAttributedString {
112    fn drop(&mut self) {
113        unsafe { objc_release(self.object) }
114    }
115}
116
117impl Into<NSAttributedString> for NSMutableAttributedString {
118    fn into(self) -> NSAttributedString {
119        unsafe { NSAttributedString::from_raw_retain(self.as_raw()) }
120    }
121}
122
123impl NSAttributedString {
124    /// Creates an attributed string with the characters of the specified string
125    /// and no attribute information.
126    pub fn new(value: &str) -> NSAttributedString {
127        let string = NSString::from(value);
128
129        unsafe {
130            let mut object: *mut Object = msg_send![class!(NSAttributedString), alloc];
131            object = msg_send![object, initWithString: string];
132            NSAttributedString { object }
133        }
134    }
135
136    /// Creates an attributed string with the specified string and attributes.
137    pub fn with_attributes(value: &str, attributes: NSAttributes) -> NSAttributedString {
138        let string = NSString::from(value);
139        let attributes = attributes.into_dictionary();
140
141        unsafe {
142            let mut object: *mut Object = msg_send![class!(NSAttributedString), alloc];
143            object = msg_send![object, initWithString: string.as_raw() attributes: attributes];
144            objc_release(attributes);
145            NSAttributedString { object }
146        }
147    }
148
149    /// Returns the size necessary to draw the string.
150    pub fn size(&self) -> CGSize {
151        unsafe { msg_send![self.object, size] }
152    }
153
154    /// Returns the bounding rectangle necessary to draw the string.
155    pub fn bounding_rect_with_size(&self, size: CGSize) -> CGRect {
156        unsafe {
157            msg_send![ self.object, boundingRectWithSize: size
158                                                 options: 1 as usize
159                                                 context: std::ptr::null_mut::<()>() ]
160        }
161    }
162}
163
164unsafe impl Send for NSAttributedString {}
165unsafe impl Sync for NSAttributedString {}
166
167impl Raw for NSAttributedString {
168    unsafe fn from_raw(object: *mut Object) -> Self {
169        NSAttributedString { object }
170    }
171
172    unsafe fn as_raw(&self) -> *mut Object {
173        self.object
174    }
175}
176
177impl Clone for NSAttributedString {
178    fn clone(&self) -> Self {
179        unsafe { Self::from_raw_retain(self.as_raw()) }
180    }
181}
182
183impl Drop for NSAttributedString {
184    fn drop(&mut self) {
185        unsafe { objc_release(self.object) }
186    }
187}