Can't centre title text in a navigation bar - ios

I can't get the title text displaying in the navigation bar to be centered. In general, but also specifically if I add a left bar button item then the title text shifts to the right.
I have a segmented control and depending upon which segment the user chooses the title text can change, and with one of the segments a left bar button will appear, then disappear if the user chooses another segment. The code to add the title is:
- (void) setHeader
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
switch (self.filterType)
{
case filterx:
label.text = #"the title":
break;
// etc.
}
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
self.navigationItem.titleView = label;
}
I've tried experimenting setting self.navigationItem.titleView.center explicitly, or to the centre of the view, or to the centre of the navigation bar but none of that makes any difference.

This may happen because there may be some other view like leftBarButton, backButton or rightBarButton which causing this issue. Actually you are creating label of width 320.0f which is the complete width of navigation bar. You need to deduct the size of other views of navigation bar from you label width and it will work for you.

Related

Presenting UIAlertController increases UILabel font size inside UIToolbar in iOS 11

I have an issue with font size of UILabel, which is programmatically added in UIToolbar:
- (UILabel *)createTitleLabel
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_topToolbar.frame)/3, CGRectGetHeight(_topToolbar.frame))];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithWhite:0.1 alpha:1.0];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:#"ArialMT" size:18];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.8;
label.lineBreakMode = NSLineBreakByTruncatingTail;
return label;
}
// -----
UILabel *label = [self createTitleLabel];
self.titleLabel = label;
[self.topToolbar insertItem:[[UIBarButtonItem alloc] initWithCustomView:label] atIndex:_topToolbar.items.count/2 animated:NO];
// ------ code for adding item to toolbar
- (void)insertItem:(UIBarItem *)barItem atIndex:(NSUInteger)index animated:(BOOL)animated
{
NSMutableArray *toolbarItems = [self.items mutableCopy];
NSAssert(index <= toolbarItems.count, #"Invalid index for toolbar item");
[toolbarItems insertObject:barItem atIndex:index];
[self setItems:toolbarItems animated:animated];
}
After setting a bit large text to title label, it works as expected, font is reduced, and tail is truncated, but when I present UIAlertController, this font is getting larger, and UILabel width is growing, which hides other bar button items inside toolbar.
Beginning with iOS 11, views added to toolbars as UIBarButtonItem using customView initializer are now laid out using auto layout. You should add sizing constraints on your label. For example:
[label.widthAnchor constraintEqualToConstant:CGRectGetWidth(_topToolbar.frame)/3].active = YES;
[label.heightAnchor constraintEqualToConstant:CGRectGetHeight(_topToolbar.frame)].active = YES;
Otherwise, auto layout will use the intrinsic content size of your label which is causing the label.adjustsFontSizeToFitWidth = YES; to be ignored. Sizing likely works initially because adding a bar button item doesn't trigger a layout pass, however presenting and dismissing another view controller will cause auto layout to perform a pass for your view controller's entire view hierarchy.
For more information see the WWDC 2017 session Updating your app for iOS 11.

objc - UITableView Title too wide.. What are my options?

I have a UITableView where I would like to set the tableView title to a wide label.
I'm trying to do something like:
"Daily Schedule - Long Station Name"
Ideally, this would mean a title and a subTitle. As there isn't a subtitle field, is there anyway to display a wide label?
Please note that I am not asking about UITableView Section Headers.
This is about the TableViewControllertitle.
Thanks
I think you are referring to the title displayed by the UINavigationController. In that case, you can get away with a custom title view, in which you can have two lines in the text label. Here is an example I'm currently using.
// Title label is the view we will assign to the titleView property
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 50)];
titleLabel.numberOfLines = 2;
titleLabel.text = #"Daily Schedule - Long Station Name";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"Montserrat-Regular" size:22];
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.minimumScaleFactor = 0.8;
titleLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
Edit or delete the attributes to get the effects you want with your title.

Adjust font size in Navigation Title

Is there any way to make the font of the title of the navigation bar adjusted with the width?
Same behaviour as a [UILabel setAdjustsFontSizeToFitWidth:YES].
I'm trying setting the appearance of the NavigationBar, but no success so far.
Try doing something like:
self.navigationItem.titleView = titleLabel;
where titleLabel is a custom UILabel that you've initialized and customized.
Check out this Fontful code, for example.
There's no way to do this with the standard title item. You can still add your custom UILabel to the navigation bar and take care of the title changes.
You can not change the font of title.
Instead try out this..
self.title = #"My title is this.";
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = label;
label.text = self.title;
[label release];

UITableView controller removes custom NavBar TitleView

I have a bit of code that allows me to customize the Navigation Bar title of a view to allow for shrinking / truncating of long titles. The code works fine almost everywhere, however when it's used in a UITableViewController, the title never appears.
The code in question is:
- (void)viewDidLoad
{
[super viewDidLoad];
//Nav bar title for truncating longer titles
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 10.0f;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.text = #"This is a really long title. It will shrink and eventually get truncated correctly.";
self.navigationItem.titleView = label;
}
I've put together a small demo project that illustrates the problem.
https://sites.google.com/site/coffeestainit/files/NavTitleError.zip
I just went through your project. The problem is that in your storyboard, you have not connected your tableViewController to the FirstViewController. So the viewDidLoad of the
FirstViewController is never called.
When I set the custom class of your TableViewController to FirstViewController in the storyboard, it set the label as title, as expected

Clipping of UILabel when adjustsFontSizeToFitWidth

When I add text to a label with the adjustsFontSizeToFitWidth set to YES the text is no longer centred vertically and eventually clips the text at the bottom of the label frame. For a large amount of text it will eventually disappear off the bottom of the label.
This is what happens if you add less text:
This is clipped as I would expect it (i.e. the font size did not reduce, the text was vertically centred in the label and clipped on the top and bottom.
Here is the code to reproduce:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blueColor];
testLabel = [[UILabel alloc] init];
testLabel.font = [UIFont boldSystemFontOfSize:172];
testLabel.textColor = [UIColor blackColor];
testLabel.adjustsFontSizeToFitWidth = YES;
testLabel.numberOfLines = 1;
testLabel.frame = CGRectMake(50, 50, 300, 100);
testLabel.text = #"123";
[self.view addSubview:testLabel];
}
Should this happen? And how do I get my label to centre vertically irrespective of the number of characters in my label.
Add
testLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
to your code to vertical-center the text on font scale.
Would also like add that adjustsFontSizeToFitWidth doesn't work too well with attributed text, so add your attributes to the label instead of the attributed text if you can. This worked for me.

Resources