Ios 更改UIBarButtonItem的字体
Ios 更改UIBarButtonItem的字体,ios,cocoa-touch,uibarbuttonitem,Ios,Cocoa Touch,Uibarbuttonitem,我的UIToolbar标题为Done中有一个UIBarButtonItem。现在我想用粗体将字体从默认更改为“Trebuchet MS”。我该怎么做呢?因为uibarbuttonite继承了UIBarItem,你可以试试 - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state 但这只适用于iOS5。对于iOS 3/4,您必须使用自定义视图
我的
UIToolbar
标题为Done中有一个UIBarButtonItem
。现在我想用粗体将字体从默认更改为“Trebuchet MS”。我该怎么做呢?因为uibarbuttonite继承了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];
为方便起见,以下是Swift实施:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
对于那些有兴趣在整个应用程序中使用
UIAppearance
来设计其uibarbuttoneim
字体的人,可以使用以下代码行完成:
目标C:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
Swift 2.3:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
Swift 3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
Swift 4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
或者对于单个UIBarButtonItem(并非适用于所有应用程序范围),如果您有一个按钮的自定义字体:
Swift 3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
Swift 4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
当然,您可以将字体和大小更改为您想要的任何大小。我更喜欢将此代码放入AppDelegate.m
文件的didfishlaunchwithoptions
部分
可用属性(只需将它们添加到NSDictionary
):
:使用NSFontAttributeName
UIFont
:使用NSForegroundColorAttributeName
UIColor更改颜色
:添加放置阴影(请参见NSShadow
(针对iOS7+更新)为一些
uibarbuttonims
执行此操作,但并非所有我推荐的方法
UIBarButtonItem
子类。不要向其添加任何内容-您只能将其用作故事板中的自定义类及其外观代理UIBarButtonItems
的自定义类更改为子类uiBarButtonim
子类,并将以下行添加到应用程序:didFinishLaunchingWithOptions:
[[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];
在Swift中,您可以按照以下步骤进行操作:
backButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
NSForegroundColorAttributeName : UIColor.blackColor()],
forState: UIControlState.Normal)
Swift3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
swift
barbutton.setTitleTextAttributes([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
forState: UIControlState.Normal)
目标-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)
}
斯威夫特3
barButtonName.setTitleTextAttributes( [NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal)
在Swift 4中,您可以通过添加以下代码来更改
UIBarButtonItem
的字体和颜色
addTodoBarButton.setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.black
], for: .normal)
这是正确的方法:
声明您的barButtonItem(在本例中为rightBarButtonItem)并将其添加到setTitleTextAttributes
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))
之后可以添加标题属性
navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)
您可以更改大小、重量(.bold、.heavy、.regular等)以及您喜欢的颜色。。。
希望有此帮助:)为了完成此任务,我想添加此方法,2019年仍在Objective-C中使用。:)
Swift 5实施
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
为IOS7开发时,应使用NSFontAttributeName代替UITextAttributeFont,并且应使用NSForegroundColorAttributeName代替UITextAttributeTextColor。UITextAttribute。。从iOS7开始,密钥已弃用。使用NSFontAttribute。。。还有,前景。。。我已经更新了iOS7中引入的弃用。谢谢你提醒我,@EmmanuelAy!好极了我的应用现在看起来很漂亮(好吧,用我自己的方式!!:P)谢谢你不要忘记字体后面的感叹号(!)。错误消息:“不可转换为字符串”并没有真正的帮助。