Skip to main content

polyhorn_ios_sys/foundation/
paragraph_style.rs

1use objc::runtime::*;
2use objc::*;
3
4use crate::Raw;
5
6/// Constants that specify text alignment.
7#[repr(usize)]
8pub enum NSTextAlignment {
9    /// Text is visually left aligned.
10    Left = 0,
11
12    #[cfg(target_os = "ios")]
13    /// Text is visually center-aligned.
14    Center = 1,
15
16    #[cfg(not(target_os = "ios"))]
17    /// Text is visually right-aligned.
18    Right = 1,
19
20    #[cfg(target_os = "ios")]
21    /// Text is visually right-aligned.
22    Right = 2,
23
24    #[cfg(not(target_os = "ios"))]
25    /// Text is visually center-aligned.
26    Center = 2,
27}
28
29/// The paragraph or ruler attributes for an attributed string.
30pub struct NSParagraphStyle {
31    object: *mut Object,
32}
33
34/// An object for changing the values of the subattributes in a paragraph style
35/// attribute.
36pub struct NSMutableParagraphStyle {
37    object: *mut Object,
38}
39
40impl NSMutableParagraphStyle {
41    /// Initializes a newly allocated mutable paragraph style.
42    pub fn new() -> NSMutableParagraphStyle {
43        unsafe {
44            let mut object: *mut Object = msg_send![class!(NSMutableParagraphStyle), alloc];
45            object = msg_send![object, init];
46            NSMutableParagraphStyle { object }
47        }
48    }
49
50    /// Sets the text alignment of the paragraph.
51    pub fn set_alignment(&mut self, alignment: NSTextAlignment) {
52        unsafe {
53            let _: () = msg_send![self.object, setAlignment: alignment];
54        }
55    }
56}
57
58impl Raw for NSMutableParagraphStyle {
59    unsafe fn from_raw(object: *mut Object) -> Self {
60        NSMutableParagraphStyle { object }
61    }
62
63    unsafe fn as_raw(&self) -> *mut Object {
64        self.object
65    }
66}
67
68impl Drop for NSMutableParagraphStyle {
69    fn drop(&mut self) {
70        unsafe {
71            objc_release(self.object);
72        }
73    }
74}
75
76impl Into<NSParagraphStyle> for NSMutableParagraphStyle {
77    fn into(self) -> NSParagraphStyle {
78        unsafe { NSParagraphStyle::from_raw_retain(self.as_raw()) }
79    }
80}
81
82impl Raw for NSParagraphStyle {
83    unsafe fn from_raw(object: *mut Object) -> Self {
84        NSParagraphStyle { object }
85    }
86
87    unsafe fn as_raw(&self) -> *mut Object {
88        self.object
89    }
90}
91
92impl Clone for NSParagraphStyle {
93    fn clone(&self) -> Self {
94        unsafe { Self::from_raw_retain(self.as_raw()) }
95    }
96}
97
98impl Drop for NSParagraphStyle {
99    fn drop(&mut self) {
100        unsafe { objc_release(self.object) }
101    }
102}