Current Version 0.5

Check out the tutorial, resources     and examples that are available for MacRuby.

MacRuby Events

19-21 Nov 2009 » RubyConf
San Francisco, CA
Laurent Sansonetti presents MacRuby

19-21 Nov 2009 » RubyConf
San Francisco, CA
Matt Aimonetti talks about writing 2D video games with MacRuby

Embed a custom font

By Matt Aimonetti

Let say you want to release your MacRuby app and use a custom embedded font?
You probably don’t want to force your users to install the font.
Well, don’t worry, just put the font file in your resources folder and use the following code:

font_location = NSBundle.mainBundle.pathForResource('MyCustomFont', ofType: 'ttf')
font_url = NSURL.fileURLWithPath(font_location)
# in MacRuby, always make sure that cocoa constants start by an uppercase
CTFontManagerRegisterFontsForURL(font_url, KCTFontManagerScopeProcess, nil)

That’s it, now your custom font is available and you can change your textfield instance’s font like that:

text_field.font = NSFont.fontWithName('MyCustomFont', size:24)

The only tricky things here were to know the Cocoa API call and to know that even if the Cocoa API references the constant to use as kCTFontManagerScopeProcess, because in Ruby, constants start by an uppercase, you need to convert it to: KCTFontManagerScopeProcess.


Original post