回答
因为从的UIBarButtonItem继承UIBarItem,你可以尝试
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
,但这仅仅是为了iOS5的。对于iOS 3/4,您将不得不使用自定义视图。
UIBarButton
有没有属性相关的更改字体。但是你可以创建一个带有自定义字体的按钮,然后添加到UIBarButton中。它可能会解决你的问题
假设你想支持iOS4和更早版本,你最好的选择是使用initWithCustomView:
方法创建一个条形按钮,并提供你自己的视图,可以像UIButton那样,你可以轻松地定制字体。
如果您想通过拖放而非编程方式创建按钮,还可以将UIButton拖到Interface Builder中的工具栏或导航栏上。
不幸的是,这意味着你自己创建按钮背景图像。没有办法在iOS5之前自定义标准UIBarButtonItem的字体。
您可以创建自定义编程UIView
:
UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
然后添加图像,标签或任何你喜欢你的自定义视图:
[buttonItemView addSubview:customImage];
[buttonItemView addSubview:customLabel];
...
现在把它放在你的UIBarButtomItem
。
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
最后添加barButtonItem到你的导航栏。
准确地说,这是可以做到如下
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
或者与对象文字语法:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
为了方便起见,这里的迅速实施:
buttonItem.setTitleTextAttributes([
NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSForegroundColorAttributeName: UIColor.greenColor()],
forState: UIControlState.Normal)
对于那些有兴趣使用UIAppearance
来设计他们的UIBarButtonItem
的字体在整个t他的应用程序,它可以使用这行代码来实现:
目标C:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
夫特2。3:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
夫特3.0/4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
或单个的UIBarButtonItem(不适用于所有应用宽),如果有用于在特定的一个按钮自定义字体:
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
当然,你可以改变字体大小&任何你想要的。我更愿意将此代码放在didFinishLaunchingWithOptions
部分的AppDelegate.m
文件中。
可用属性(只是把它们添加到NSDictionary
):
NSFontAttributeName
:更改字体与UIFont
NSForegroundColorAttributeName
:用UIColor
NSShadow
更改颜色:添加阴影(见NSShadow
class reference)
(已更新为iOS7 +)
要做到这一点UIBarButtonItems
但不是所有我推荐以下方法。
- 创建一个
UIBarButtonItem
子类。对于所有期望UIBarButtonItems
到子类 - 你只会用它作为故事板和它的外观代理的自定义类...
- 在你的故事板,更改自定义类 - 不要添加任何东西给它AppDelegate中导入
UIBarButtonItem
子类,下面一行添加到application:didFinishLaunchingWithOptions:
在我来说,我子类UIBarButtonItem
为粗体的唯一目的的字符串:
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
这些都是上面的很好的答案。刚刚更新了iOS7:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
在斯威夫特你这样做如下:
backButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
NSForegroundColorAttributeName : UIColor.blackColor()],
forState: UIControlState.Normal)
不要忘记字体后面的感叹号(!)。错误消息:“'_'不能转换为'String'”并不真正有用。 – Ciryon 2015-09-03 06:41:20
Swift3
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
迅速
barbutton.setTitleTextAttributes([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
forState: UIControlState.Normal)
Objective-C的
[ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
[UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
nil]
forState:UIControlStateNormal];
在整个应用程序:
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
SWIFT 3
barButtonName.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal)
- 1. 更改字体UIBarButtonItem
- 2. 更改UIBarButtonItem颜色
- 3. 更改UIBarButtonItem颜色
- 4. 更改UIBarButtonItem的宽度
- 5. UIBarButtonItem更改viewController的方向
- 6. 更改UIBarButtonItem的位置
- 7. 更改UIBarButtonItem后退按钮
- 8. iOS版 - 更改的UIBarButtonItem的高度
- 9. iOS:无法更改UIBarButtonItem的颜色
- 10. 如何更改UIBarButtonItem的选择区域?
- 11. UINavigationBar - 如何更改UIBarButtonItem的位置?
- 12. 无法更改UIBarButtonItem的背景
- 13. 如何更改UIBarButtonItem的颜色?
- 14. 更改完成UIBarButtonItem的背景图像
- 15. 更改UIBarButtonItem的字体颜色
- 16. 无法更改UIBarButtonItem的图像
- 17. iOS无法正确更改UIBarButtonItem图像
- 18. swift xcode更改尺寸为navigationItem.leftBarButtonItem = UIBarButtonItem
- 19. 在iOS 10中更改UIBarButtonItem字体
- 20. 更改UIBarButtonItem标题看起来不好
- 21. 如何更改UIBarButtonItem按钮设计?
- 22. 如何更改图像并禁用UIBarButtonItem
- 23. 如何更改UIBarButtonItem标题 - iPhone SDK
- 24. 如何更改UIBarButtonItem图像大小
- 25. 以编程方式更改UIBarButtonItem状态
- 26. 使用图像更改uibarbuttonitem背景
- 27. 将UIBarButtonItem更改为简单图标
- 28. 如何更改Swift中的UIBarButtonItem的文本
- 29. 我如何更改代码中的UIBarButtonItem的样式
- 30. 如何在运行时更改UINaviagationBar中的UIBarButtonItem类型?
套住的是,我有一个问题,它现在是工作的罚款。 – 2012-10-12 18:03:01
啊哈!最后,我发现你@teriiehina! – Martin 2015-04-27 16:09:05
@Martin我没有隐藏:) – teriiehina 2015-04-27 18:15:42