I'm trying to customize the navigation bar in a RubyMotion app but can't seem to change the title text font or color. I can set the background image and tint but not the title attributes.
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
navigationBar = UINavigationBar.appearance
#navigationBar.setBackgroundImage(UIImage.imageNamed('navigation-bar-background.png'), forBarMetrics: UIBarMetricsDefault)
#navigationBar.setTintColor(UIColor.greenColor)
navigationBar.setTitleTextAttributes({
UITextAttributeFont: UIFont.fontWithName('Trebuchet MS', size:24),
UITextAttributeTextShadowColor: UIColor.colorWithWhite(0.0, alpha:0.4),
UITextAttributeTextColor: UIColor.blueColor
})
puts navigationBar.titleTextAttributes.inspect
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(MainMenuController.alloc.init)
@window.rootViewController.wantsFullScreenLayout = true
@window.makeKeyAndVisible
true
end
end
The console output shows this for the inspect statement:
{:UITextAttributeFont=>#<UICFFont:0x963aa70>,
:UITextAttributeTextShadowColor=>UIColor.color(0x0, 0.0),
:UITextAttributeTextColor=>UIColor.blueColor}
So it appears that everything is being set correctly but all I get is the standard blue navigation bar with white text.
UITextAttributeFont
, etc. are actual constants, but when you use them in a newer Ruby 1.9-style hash assignment, it's converting them to symbols.
The easiest way around this is to use the older hash-rocket assignment style (make sure to not put colons in front of the UIText... items):
navigationBar.setTitleTextAttributes({
UITextAttributeFont => UIFont.fontWithName('Trebuchet MS', size:24),
UITextAttributeTextShadowColor => UIColor.colorWithWhite(0.0, alpha:0.4),
UITextAttributeTextColor => UIColor.blueColor
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With