2

我的 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]];

构建成功,但是当它在我的 iPod 上运行时,它在调试控制台上给了我这个错误消息:

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) 

我认为这是由这行代码引起的:

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

因为当我评论那行时,它运行完美而没有错误。但导航栏按钮的字体是默认字体。如何定制它?

谢谢你

4

3 回答 3

6

您是否尝试过类似的方法:

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

这也允许您编辑其他属性,例如阴影等。 来自这个很棒的教程

于 2013-09-10T09:49:36.500 回答
1

尝试这个

可用的 UITextAttributes(只需将它们添加到 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];
于 2013-09-10T10:26:30.547 回答
0

来自苹果文档:

字体

标题的字体。

(在 iOS 3.0 中已弃用。相反,设置分配给 textLabel 和 detailTextLabel 属性的 UILabel 对象的字体。)

@property(nonatomic, 保留) UIFont *font

但是,这样做会有所帮助:

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
于 2013-09-10T09:49:21.260 回答