如何在UIBarButtonItem上设置字体大小? [英] How do you set the font size on a UIBarButtonItem?
本文介绍了如何在UIBarButtonItem上设置字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到在自定义UIBarButtonItem中设置标题字体大小的方法。我能想到解决这个问题的唯一方法是将其设置为我想避免的图像。还有其他建议吗?
I can't find a way to set the font size of the title in a custom UIBarButtonItem. The only way I can think of getting around this is to set it as an image which I would like to avoid. Any other suggestions?
推荐答案
简单来说,只需:
Objective-C :
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;
这篇关于如何在UIBarButtonItem上设置字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文