Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

ios - How do you set the font size on a 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?

question from:https://stackoverflow.com/questions/2720733/how-do-you-set-the-font-size-on-a-uibarbuttonitem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...