Ank*_*yas 130 cocoa-touch uibarbuttonitem ios
我有一个UIBarButtonItem
在我的UIToolbar
标题做了.现在我想用Bold 将字体从默认字体更改为"Trebuchet MS".我怎样才能做到这一点?
mas*_*ask 207
确切地说,这可以如下完成
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
或者使用对象文字语法:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
为方便起见,这是Swift实现:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
ter*_*ina 125
因为UIBarButtonItem继承自UIBarItem,所以您可以尝试
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
Run Code Online (Sandbox Code Playgroud)
但这仅适用于iOS5.对于iOS 3/4,您必须使用自定义视图.
rog*_*rog 122
对于那些有兴趣在整个应用程序中使用UIAppearance
它们UIBarButtonItem
的字体样式的人来说,可以使用以下代码行完成:
目标C:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Swift 2.3:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
Run Code Online (Sandbox Code Playgroud)
或者对于单个UIBarButtonItem(不适用于所有应用程序范围),如果您特别为一个按钮设置了自定义字体:
斯威夫特3
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Run Code Online (Sandbox Code Playgroud)
当然,您可以将字体和大小更改为您想要的任何内容.我更喜欢将此代码AppDelegate.m
放在该didFinishLaunchingWithOptions
部分的文件中.
可用属性(只需将它们添加到NSDictionary
):
NSFontAttributeName
:用a更改字体 UIFont
NSForegroundColorAttributeName
:改变颜色 UIColor
NSShadow
:添加投影(请参阅NSShadow
类引用)(针对iOS7 +更新)
Ant*_*ine 19
在Swift中你会这样做:
backButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
NSForegroundColorAttributeName : UIColor.blackColor()],
forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
Dav*_*nte 16
以上是很好的答案.只需更新iOS7:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Anb*_*hik 15
Swift3
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
迅速
barbutton.setTitleTextAttributes([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
Objective-C的
[ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
[UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Swift 5 实现
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
在Swift 4中,您可以UIBarButtonItem
通过添加以下代码来更改的字体和颜色。
addTodoBarButton.setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.black
], for: .normal)
Run Code Online (Sandbox Code Playgroud)
为了一些UIBarButtonItems
但不是全部这样做我推荐以下方法.
UIBarButtonItem
子类.不要添加任何东西 - 你只会将它用作故事板中的自定义类及其外观代理...UIBarButtonItems
子类UIBarButtonItem
子类并添加以下行application:didFinishLaunchingWithOptions:
在我的情况下,我将子类UIBarButtonItem
化为仅用于加粗文本的目的:
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
这是正确的方法:声明您的 barButtonItem(在本例中为 rightBarButtonItem)并添加它 setTitleTextAttributes。
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))
Run Code Online (Sandbox Code Playgroud)
添加标题属性后
navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)
Run Code Online (Sandbox Code Playgroud)
您可以更改尺寸、重量(.bold、.heavy、.regular 等)以及您喜欢的颜色...希望这有帮助:)
归档时间: |
|
查看次数: |
80850 次 |
最近记录: |