core_foundation/
attributed_string.rs1pub use core_foundation_sys::attributed_string::*;
11
12use crate::base::TCFType;
13use crate::string::{CFString, CFStringRef};
14use core_foundation_sys::base::{kCFAllocatorDefault, CFIndex, CFRange};
15use std::ptr::null;
16
17declare_TCFType! {
18 CFAttributedString, CFAttributedStringRef
19}
20impl_TCFType!(
21 CFAttributedString,
22 CFAttributedStringRef,
23 CFAttributedStringGetTypeID
24);
25
26impl CFAttributedString {
27 #[inline]
28 pub fn new(string: &CFString) -> Self {
29 unsafe {
30 let astr_ref =
31 CFAttributedStringCreate(kCFAllocatorDefault, string.as_concrete_TypeRef(), null());
32
33 CFAttributedString::wrap_under_create_rule(astr_ref)
34 }
35 }
36
37 #[inline]
38 pub fn char_len(&self) -> CFIndex {
39 unsafe { CFAttributedStringGetLength(self.0) }
40 }
41}
42
43declare_TCFType! {
44 CFMutableAttributedString, CFMutableAttributedStringRef
45}
46impl_TCFType!(
47 CFMutableAttributedString,
48 CFMutableAttributedStringRef,
49 CFAttributedStringGetTypeID
50);
51
52impl CFMutableAttributedString {
53 #[inline]
54 pub fn new() -> Self {
55 unsafe {
56 let astr_ref = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
57
58 CFMutableAttributedString::wrap_under_create_rule(astr_ref)
59 }
60 }
61
62 #[inline]
63 pub fn char_len(&self) -> CFIndex {
64 unsafe { CFAttributedStringGetLength(self.0) }
65 }
66
67 #[inline]
68 pub fn replace_str(&mut self, string: &CFString, range: CFRange) {
69 unsafe {
70 CFAttributedStringReplaceString(self.0, range, string.as_concrete_TypeRef());
71 }
72 }
73
74 #[inline]
75 pub fn set_attribute<T: TCFType>(&mut self, range: CFRange, name: CFStringRef, value: &T) {
76 unsafe {
77 CFAttributedStringSetAttribute(self.0, range, name, value.as_CFTypeRef());
78 }
79 }
80}
81
82impl Default for CFMutableAttributedString {
83 fn default() -> Self {
84 Self::new()
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn attributed_string_type_id_comparison() {
94 assert_eq!(
97 <CFAttributedString as TCFType>::type_id(),
98 <CFMutableAttributedString as TCFType>::type_id()
99 );
100 }
101}