我有一个UIBarButtonItem
在我的UIToolbar
题为完成。现在,我想使用粗体将字体从默认更改为“ Trebuchet MS”。我怎样才能做到这一点?
我有一个UIBarButtonItem
在我的UIToolbar
题为完成。现在,我想使用粗体将字体从默认更改为“ Trebuchet MS”。我怎样才能做到这一点?
Answers:
确切地说,这可以按以下方式完成
[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
样式的UIBarButtonItem
人,可以使用以下代码行完成:
目标C:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Swift 2.3:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
迅捷3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
斯威夫特4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
或对于单个UIBarButtonItem(并非对于整个应用程序都适用),如果您特别为一个按钮使用了自定义字体:
迅捷3
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
斯威夫特4
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
当然,您可以将字体和大小更改为所需的字体。我更喜欢将此代码AppDelegate.m
放在该didFinishLaunchingWithOptions
部分的文件中。
可用属性(只需将它们添加到中NSDictionary
):
NSFontAttributeName
:使用更改字体 UIFont
NSForegroundColorAttributeName
:用 UIColor
NSShadow
:添加投影(请参阅NSShadow
类参考)(已针对iOS7 +更新)
这些是上面的绝妙答案。刚刚针对iOS7更新:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
迅捷3
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)
目标C
[ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
[UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
nil]
forState:UIControlStateNormal];
为此,UIBarButtonItems
我建议采用以下方法,但不是全部。
UIBarButtonItem
子类。不要在其中添加任何内容-您只会将其用作情节提要中的自定义类及其外观代理...UIBarButtonItems
子类所需的所有自定义类UIBarButtonItem
子类,并将以下行添加到application:didFinishLaunchingWithOptions:
就我而言UIBarButtonItem
,仅出于加粗文本的目的而将其归类:
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
这是正确的方法:声明您的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等)和颜色。希望此帮助:)
Swift 5实施
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
整个应用程序:
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
您可以通过UIView
编程方式创建自定义:
UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
然后将图像,标签或任何您喜欢的内容添加到自定义视图:
[buttonItemView addSubview:customImage];
[buttonItemView addSubview:customLabel];
...
现在把它放在你的UIBarButtomItem
。
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
最后,将barButtonItem添加到导航栏。
为了完成,我想添加这种方法,该方法仍将在2019年的Objective-C中使用。:
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.text = _titleBarButtonItem.title;
_titleLabel.textColor = UIColor.whiteColor;
_titleLabel.font = [UtilityMethods appFontProDisplayBold:26.0];
[_titleLabel sizeToFit];
UIBarButtonItem *titleLabelItem = [[UIBarButtonItem alloc] initWithCustomView:_titleLabel];