polyhorn_ios_sys/foundation/
attributed_string.rs1use objc::runtime::*;
2use objc::*;
3
4use super::{NSParagraphStyle, NSString};
5use crate::coregraphics::{CGRect, CGSize};
6use crate::uikit::{UIColor, UIFont};
7use crate::Raw;
8
9pub struct NSAttributes {
11 pub font: UIFont,
16
17 pub foreground_color: UIColor,
22
23 pub paragraph_style: NSParagraphStyle,
29}
30
31extern "C" {
32 static NSFontAttributeName: *mut Object;
37
38 static NSForegroundColorAttributeName: *mut Object;
43
44 static NSParagraphStyleAttributeName: *mut Object;
50}
51
52impl NSAttributes {
53 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
69pub struct NSAttributedString {
72 object: *mut Object,
73}
74
75pub struct NSMutableAttributedString {
79 object: *mut Object,
80}
81
82impl NSMutableAttributedString {
83 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 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 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 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 pub fn size(&self) -> CGSize {
151 unsafe { msg_send![self.object, size] }
152 }
153
154 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}