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

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.

Related

Dynamically adjust font size in UINavigationBar

What I want to do is this :
I've a navigation bar using an image covering the whole bar and looking like this:
Now I want to put the title on the black part of the bar and so it doesn't impinge on the icon.
This title may be very short or very long. So I tried this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
label.backgroundColor = [UIColor blackColor];
label.numberOfLines = 2;
label.minimumFontSize = 8.;
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text = name;
self.navigationItem.titleView = label;
But this is not working because the label's width grows if needed upon the label and exceeds 200px.
What should I do then?
Thanks for your help.
PS : the camera icon is not a button.
Your code works fine in iOS 7.0
Since you are using UITextAlignmentCenter (instead of NSTextAlignmentCenter), and also using minimumFontSize, I am assuming you are running on an earlier version of iOS, since these were both deprecated in iOS 6.0
The main problem is that adjustsFontSizeToFitWidth only works in iOS6 and earlier if the numberOfLines is set to 1
As you can see in the apple developer documentation
https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth
If you necessarily want two lines and also want the font size to adjust... you could maybe just use the length property of your name string, which would tell you how many characters are in the string, and you could then set the font size according to the length of the string

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

Can't centre title text in a navigation bar

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.

How to disable UITableView header scroll?

I created a UITableView programmatically and now I want to add a fixed header to it.
As I the below code the header also scrolls with my tableview.
UILabel *label = [[[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
label.backgroundColor = [UIColor darkGrayColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8];
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"my header";
myTable.tableHeaderView = label;
Is there a function which disables header scrolling?
Thank you
No, you need to either change the view to be a section header (where it will remain at the top of the tableview while that section is visible), or you need to make the header view a sibling to the tableview in the tableview's superview -- you can create a UIView just for the purpose of holding the header view and the table view.

Resources