4

I am building an iOS app using Rubymotion and I need to set the font family to a custom font for a button in the navigation bar (UIBarButtonItem). I know I do it like this in Objective-C but how is it done in Ruby?

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];

Thankful for all help!

Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175
  • just curious. Why Rubymotion instead of Xcode? because of the Objective-C learning curve? – Raptor Nov 19 '12 at 08:51
  • I know Ruby very good (Rails developer) and I see no reason to use Objective-C since the outcome from both Rubymotion and Objective-C is exactly the same. – Jonathan Clark Nov 19 '12 at 09:01
  • just discussion: Rubymotion can't build Interface easily, right? (need to build via 100% codes only, i.e. no drag & drop) – Raptor Nov 19 '12 at 09:25
  • 1
    No, you can use both Storyboards and Nib files. They are compiled automatically with Rubymotion. – Jonathan Clark Nov 19 '12 at 09:52

2 Answers2

2

I think this will do it. I don't have my Mac handy to test this out so give it a shot and let us know how it goes.

buttonItem.setTitleTextAttributes({UITextAttributeFont => UIFont.fontWithName("Helvetica-Bold", size:26.0)}, forState:UIControlStateNormal)
vacawama
  • 150,663
  • 30
  • 266
  • 294
2

In your AppDelegate use something like this

NSDictionary *itemTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIColor whiteColor],NSForegroundColorAttributeName,
                                    [UIColor whiteColor],NSBackgroundColorAttributeName,
                                    [UIFont fontWithName:@"Ubuntu-Regular" size:18],NSFontAttributeName,
                                    nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:itemTextAttributes forState:UIControlStateNormal];
Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35