use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};
#[allow(non_snake_case)]
pub trait NSAttributedString: Sized {
unsafe fn alloc(_: Self) -> *mut Object {
msg_send![class!(NSAttributedString), alloc]
}
unsafe fn init_attributed_string(self, string: *mut Object) -> *mut Object;
unsafe fn appendAttributedString_(self, attr_string: *mut Object);
unsafe fn RTFDFromRange_documentAttributes_(
self,
range: super::NSRange,
attrs: *mut Object,
) -> *mut Object;
unsafe fn RTFFromRange_documentAttributes_(
self,
range: super::NSRange,
attrs: *mut Object,
) -> *mut Object;
unsafe fn string(self) -> *mut Object;
}
impl NSAttributedString for *mut Object {
unsafe fn init_attributed_string(self, string: *mut Object) -> *mut Object {
msg_send![self, initWithString: string]
}
unsafe fn appendAttributedString_(self, attr_string: *mut Object) {
let _: () = msg_send![self, appendAttributedString: attr_string];
}
unsafe fn RTFDFromRange_documentAttributes_(
self,
range: super::NSRange,
attrs: *mut Object,
) -> *mut Object {
msg_send![self, RTFDFromRange: range documentAttributes: attrs]
}
unsafe fn RTFFromRange_documentAttributes_(
self,
range: super::NSRange,
attrs: *mut Object,
) -> *mut Object {
msg_send![self, RTFFromRange: range documentAttributes: attrs]
}
unsafe fn string(self) -> *mut Object {
msg_send![self, string]
}
}
pub trait NSMutableAttributedString: NSAttributedString {
unsafe fn alloc(_: Self) -> *mut Object {
msg_send![class!(NSMutableAttributedString), alloc]
}
}
impl NSMutableAttributedString for *mut Object {}
#[cfg(test)]
mod tests {
use super::*;
use objc::runtime::Object;
use objc2_foundation::NSSize;
use std::ptr;
use crate::mac::{NSRange, ns_string};
type Id = *mut Object;
#[allow(non_upper_case_globals)]
const nil: Id = ptr::null_mut();
#[test]
#[ignore] fn test_nsattributed_string() {
#[allow(non_snake_case)]
pub trait NSTextAttachment: Sized {
unsafe fn alloc(_: Self) -> Id {
msg_send![class!(NSTextAttachment), alloc]
}
}
impl NSTextAttachment for Id {}
unsafe {
let image: Id = msg_send![class!(NSImage), alloc];
let image: Id = msg_send![image, initWithContentsOfFile: ns_string("test.jpeg")];
let _size: NSSize = msg_send![image, size];
let string = ns_string("Test String");
let attr_string = NSMutableAttributedString::alloc(nil).init_attributed_string(string);
let hello_string = ns_string("Hello World");
let hello_attr_string =
NSAttributedString::alloc(nil).init_attributed_string(hello_string);
attr_string.appendAttributedString_(hello_attr_string);
let attachment = NSTextAttachment::alloc(nil);
let _: () = msg_send![attachment, setImage: image];
let image_attr_string =
msg_send![class!(NSAttributedString), attributedStringWithAttachment: attachment];
attr_string.appendAttributedString_(image_attr_string);
let another_string = ns_string("Another String");
let another_attr_string =
NSAttributedString::alloc(nil).init_attributed_string(another_string);
attr_string.appendAttributedString_(another_attr_string);
let len: usize = msg_send![attr_string, length];
let rtfd_data =
attr_string.RTFDFromRange_documentAttributes_(NSRange::from(0..len), nil);
assert_ne!(rtfd_data, nil);
}
}
}