Convert CFDictionary to NSDictionary (or alternatives)

666 views
Skip to first unread message

Omar Alejandro Mainegra Sarduy

unread,
Oct 23, 2014, 2:11:29 PM10/23/14
to rob...@googlegroups.com

I need to do some high performance off-screen rendering text and want to use Core-Text, for that I need to create a NSAttributedString, something like this:

    CTFont font = CTFont.create(fontName, fontSize, null);
    NSMutableDictionary dictionary = new NSMutableDictionary<>();
    dictionary.put(new NSString("NSFont"), font);
    ...

    NSAttributedString attrString = new NSAttributedString(string, dictionary);
    CTLine line = CTLine.create(attrString);

. The thing is that NSAttributedString needs NSDictionary, but there only can put NSObjects, not CFType objects (like CTFont). Currently I create a CFDictionary, but can't find a way to convert to NSDictionary. Any help will be appreciated.

Niklas Therning

unread,
Oct 24, 2014, 1:59:09 AM10/24/14
to Omar Alejandro Mainegra Sarduy, rob...@googlegroups.com
You seem to be using an old version of RoboVM? Which version are you on? NSAttributedString has been changed in the latest versions and doesn't have a constructor that takes a dictionary any longer.

Anyway, CTFont and UIFont are toll-free bridged which means that you can simply cast a CTFont to a UIFont in Obj-C. In Java with RoboVM you can (provided that you are on a recent version of RoboVM) call the .as() method on the CTFont:

UIFont uiFont = ctFont.as(UIFont.class)

You don't want to do this in a tight loop as it uses reflection under the hood to do the "casting".

If you are on an older version of RoboVM the .as() method isn't available. You should then be able to do something like

UIFont uiFont = (UIFont) NSObject.Marshaler.toObject(UIFont.class, ctFont.getHandle() , 0);

Once you have a UIFont you can add it to the dictionary.



--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Omar Alejandro Mainegra Sarduy

unread,
Oct 27, 2014, 11:03:59 AM10/27/14
to rob...@googlegroups.com, omai...@gmail.com
Thanks for your replay, I didn't know about toll-free bridged, but I found some information in an Apple forum, then I did

CFMutableDictionary dictionary = CFMutableDictionary.create();
 .
 .
 .
NSAttributedString attrString = new NSAttributedString(string, dictionary.as(NSDictionary.class));

and works pretty well. I'm currently using version 1.0.0 alpha 1.



Reply all
Reply to author
Forward
0 new messages