2

I have this on my AppDelegate.m :

// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:18]];

build was successful but when it runs on my iPod, it gives me this error message on debugging console :

2013-09-10 16:42:19.581 PushTransition[3599:907] -[_UIBarItemAppearance setFont:]: unrecognized selector sent to instance 0x1cda7100
2013-09-10 16:42:19.586 PushTransition[3599:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIBarItemAppearance setFont:]: unrecognized selector sent to instance 0x1cda7100'
*** First throw call stack:
(0x333602a3 0x3b04497f 0x33363e07 0x33362531 0x332b9f68 0x4ff27 0x351c8ad9 0x351c8663 0x351c084b 0x35168c39 0x351686cd 0x3516811b 0x36e6f5a3 0x36e6f1d3 0x33335173 0x33335117 0x33333f99 0x332a6ebd 0x332a6d49 0x351bf485 0x351bc301 0x4fced 0x3b47bb20)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

I think this caused by this line of code :

[[UIBarButtonItem appearance] setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:18]];

because when I comment that line, it runs perfectly without error. but the navbar button's font is default font. how to customise it?

thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saint Robson
  • 5,475
  • 18
  • 71
  • 118

3 Answers3

6

Have you tried something like:

[[UIBarButtonItem appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [UIFont fontWithName:@"AmericanTypewriter" size:20.0],
        UITextAttributeFont,
        nil]
    forState:UIControlStateNormal];

This also allows you to edit other attributes such as shadow etc. From this great tutorial

Michael M
  • 1,034
  • 2
  • 8
  • 21
  • This has been updated with IOS7: `[[UIBarButtonItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"AmericanTypewriter" size:20.0], NSFontAttributeName, nil] forState:UIControlStateNormal];` – Joshua Hart Nov 01 '15 at 16:31
1

Try this

Available UITextAttributes (just add them to the NSDictionary):

UITextAttributeFont: Change font with a UIFont
UITextAttributeTextColor: Change color with a UIColor
UITextAttributeShadowColor: Change shadow color with a UIColor
UITextAttributeShadowOffset: Change shadow offset with a UIOffset

 [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Raleway-ExtraLight" size:18.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
0

From Apple Docs:

Font

The font of the title.

(Deprecated in iOS 3.0. Instead, set the fonts of the UILabel objects assigned to the textLabel and detailTextLabel properties.)

@property(nonatomic, retain) UIFont *font

Doing this however will help:

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120