Dynamically adjust font size in UINavigationBar - ios

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

Related

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.

UILabel will draw something with Chinese even if the width is equal to 0 in iOS 8

Just creat a new "Single View Application" and add the code to the viewDidLoad method, you will see it:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 0, 100)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor blackColor];
label.text = #"中文";
[self.view addSubview:label];
After viewDidLoad() is called and before the UILabel shows up on the screen,
iOS does multiple steps.
Some of them are
Calling viewWillLayoutSubviews
Calculating the layout
Calling viewDidLayoutSubviews
Calling viewWillAppear and finally
Calling viewDidAppear
Most likely your view was resized when the layout was done, please check in both, viewDidLayoutSubviews and viewDidAppear what your frame size is then.
i think you need to add below code
label.clipsToBounds = YES;
It's just an iOS bug, has been fixed in iOS 10, but still appear in the iOS 8.1.
In my case, setHidden:YES instead of setWidth:0 will solve this problem.

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];

UILabel view disappear when the height greater than 8192

Assigning large string to UILabel. And, adding this label into a scroll view.
The UILabel disappear when the UILabel height larger than 8192pt (which is 2^13).
Is this an iOS bug?
And should I use other implementation to render such amount of string?
Should I use table view with cell?
UPDATE
The code that will display the UILabel:
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.text = rumor.displayText;
label.frame = CGRectMake(0, 0, self.view.frame.size.width, 8192);
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
And the code that UILabel does disappear
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.text = rumor.displayText;
label.frame = CGRectMake(0, 0, self.view.frame.size.width, 8193);
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
First of all - it doesn't have to be a bug. It is just undefined behavior. Note that with every component, there will be some upper size limit when the component stops working correctly . 8192 points seems to be a low limit but still it's about 8 times the iPad screen in portrait mode.
You are not supposed to make views that big. Note that UIViews are often rendered into memory and buffered, to make redrawing faster. With 8192 height, the buffer will have to be very big.
Splitting the text into several UILabels (e.g. by paragraph) would definitely be an improvement.
See https://stackoverflow.com/a/1494496/669586
I ran into this same issue with UITextViews, and came up with a fairly effective solution.
If you'd like to see it, check out my answer here!:
https://stackoverflow.com/a/37147533/2155673
It should be fairly easily to adapt for UILabels.

Programmatically created UILabel not working in iOS 6

I programmatically create a few UILabels in my app. In iOS 5 this code worked fine.
In iOS 6 the labels simply don't seem to appear on the screen.
I don't see any warnings, the code definitely executes, but no label.
I've tried searching for similar issues but can't find anything. My labels have literally just disappeared.
Any help appreciated.
float popUpWidth = 700.0
//Add label
UILabel *label;
label = [[UILabel new] initWithFrame:CGRectMake(0,0,popUpWidth,50)];
label.text = #"Notes";
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor whiteColor];
//Add label to view
[self.view addSubview:label];
The problem is you are creating a label twice as wide as the devices screen, and setting a small amount of text to be centered thus causing the text to be off screen. Remember CGRect uses points not pixels. Additionally, I tested your code and had to replace "new" with "alloc" for it to work properly, I suggest you do the same.
The full correct line would be:
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,popUpWidth,50)];
Calling [UILabel new] is like calling [[UILabel alloc] init] so you should change your code like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,popUpWidth,50)];

Resources