Posts Tagged CTFontManager
MacRuby tips: embed a custom font
Posted by Matt Aimonetti in macruby on October 6th, 2009
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.
cocoa, CTFontManager, CTFontManagerRegisterFontsForURL, embedded, font, macruby, ruby
-
Tags
apple architecture book caching cocoa concurrency couchdb datamapper design documentation edge enterprise fibers foundation frameworks gil head jruby llvm macruby Meet the merbists merb merb book merbcamp merbist merge News objective-c performance rails rails3 rails 3.0 railsconf railssummit rc release revolution router ruby rubyconf scalability scriptingbridge sprint testing trainingArchives
- December 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- February 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
Blogroll
