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

时间:2010-04-27 11:34:28

标签: iphone uibarbuttonitem font-size

我找不到在自定义UIBarButtonItem中设置标题字体大小的方法。我能想到解决这个问题的唯一方法是将其设置为我想避免的图像。还有其他建议吗?

6 个答案:

答案 0 :(得分:78)

简单来说,只需:

目标-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;

夫特:

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;

答案 1 :(得分:3)

创建UILabel并使用-initWithCustomView:

答案 2 :(得分:2)

作为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]];

(当然,为了获得正确的格式,用于初始化recttxtLabel的{​​{1}}应该是正确的尺寸....但这是另一项练习! )

答案 3 :(得分: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];

答案 4 :(得分:1)

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);

答案 5 :(得分: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)