如何在UIBarButtonItem中设置字体大小?

41

我找不到一种方法来设置自定义 UIBarButtonItem 标题的字体大小。我能想到的唯一解决方法是将其设置为图像,但我希望避免这样做。有其他建议吗?


这里有一个类似的问题。http://stackoverflow.com/questions/5421121/change-font-size-via-uibarbuttonitem-in-uitextview-works-just-once - acecapades
6个回答

82

Objective-C:

NSUInteger fontSize = 20;
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
NSDictionary *attributes = @{NSFontAttributeName: font};

UIBarButtonItem *item = [[UIBarButtonItem alloc] init];

[item setTitle:@"Some Text"];
[item setTitleTextAttributes:attributes forState:UIControlStateNormal];

self.navigationItem.rightBarButtonItem = item;

Swift:

let fontSize:CGFloat = 20;
let font:UIFont = UIFont.boldSystemFont(ofSize: fontSize);
let attributes:[String : Any] = [NSFontAttributeName: font];
    
let item = UIBarButtonItem.init();
    
item.title = "Some Text";
item.setTitleTextAttributes(attributes, for: UIControlState.normal);
    
self.navigationItem.rightBarButtonItem = item;

2
你也可以使用 UIAppearance 代理来设置 setTitleTextAttributes - livingtech
4
小提示:UITextAttributeFont键值在iOS 7中已被弃用,您可以使用NSFontAttributeName代替。 - Joe Regan
如果您计划通过点击按钮打开弹出窗口或菜单,则还应为“UIControlStateHighlighted”设置文本属性。 - LaborEtArs

4
作为kennytm建议的具体示例,您可以使用以下代码创建UIBarButtonItem
UILabel *txtLabel = [[UILabel alloc] initWithFrame:rect];
txtLabel.backgroundColor = [UIColor clearColor];
txtLabel.textColor = [UIColor lightGrayColor];
txtLabel.text = @"This is a custom label";
UIBarButtonItem *btnText = [[[UIBarButtonItem alloc] initWithCustomView:txtLabel] autorelease];

然后,您可以将其作为居中文本添加到UIToolbar上,例如使用以下代码:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:rect];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *flexSpace2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];

[toolBar setItems:[NSArray arrayWithItems:flexSpace1, btnText, flexSpace2, nil]];

(当然,为了获得适当的格式,用于初始化txtLabeltoolBarrect应该是适当的大小...但这是另一个练习。)

1
你可以使用[txtLabel sizeToFit]来使其正确大小。顺便提一下,我不确定这种方法是否会保留蓝色胶囊按钮项的背景,因此您可能需要使用一些自定义图形。但这是您最好的选择,因为如果您使用标题初始化它,则无法实际设置栏按钮项的字体大小。 - axiixc
你也应该设置标签的框架大小。在我的情况下,没有显示任何项目,因为它们的宽度和高度初始化为0。 - Alexander Schmidt

3
创建一个UILabel并使用-initWithCustomView:方法。

那我是将UIBarButtonItem传递到UILabel中并正常设置字体吗?我可能还会遇到问题,因为我正在使用Interface Builder将UIBarButtonItem放入UIToolbar中。 - Jim
1
使用setTextAttributes:forState:方法。 - bigkm

3

Swift5:

let item = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(self.onItemTapped))
let font:UIFont = UIFont(name: "", size: 18) ?? UIFont()
item.setTitleTextAttributes([NSAttributedString.Key.font: font], for: UIControl.State.normal)

2
[[UIBarButtonItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                     [UIFont fontWithName:@"FONT-NAME" size:21.0], NSFontAttributeName, nil]
                                           forState:UIControlStateNormal];

1
let barButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Title",
                                                     style: .plain,
                                                     target: nil,
                                                     action: nil)
let font: UIFont = UIFont.systemFont(ofSize: 12.0)
let textAttributes: [NSAttributedString.Key: Any] = [.font: font]

barButtonItem.setTitleTextAttributes(textAttributes, for: .normal)
barButtonItem.setTitleTextAttributes(textAttributes, for: .selected)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接