How to set max width of a UIBarButtonItem
Asked Answered
P

2

7

I have a UIBarButtonItem in a UIToolbar that is updated with changes to a title represented by a text field. The text field should not have a short maximum length. When the title is quite long, the item occludes the button items to its right. How can I make it automatically truncate to a certain width? Interface Builder Simulator

Pillowcase answered 19/7, 2011 at 15:25 Comment(0)
C
5

Use a custom view with maximum possible width, textAlignment set to UITextAlignmentCenter and lineBreakMode to UILineBreakModeTailTruncation.

UILabel* l = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)]autorelease];
//200 is just a number I choose. you should calculate your maximum possible value
l.textAlignment = UITextAlignmentCenter;
l.lineBreakMode = UILineBreakModeTailTruncation;
self.navigationItem.titleView = l;
Ceraceous answered 19/7, 2011 at 15:35 Comment(2)
It looks like Helvetica 20.0 Bold matches the default.Pillowcase
UILabel* l = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)]autorelease]; //200 is just a number I choose. You should calculate your maximum possible value l.textAlignment = UITextAlignmentCenter; l.lineBreakMode = UILineBreakModeTailTruncation; l.minimumFontSize = 10; l.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];// if you need the bold version [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; l.backgroundColor = [UIColor clearColor]; self.navigationItem.titleView = l; :)Ceraceous
D
1

You can also set the width in the Storyboard Size Inspector:

enter image description here

Donoghue answered 9/4, 2012 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.