Programmatically created UILabel not working in iOS 6 - ios

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

Related

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.

UITextField sizeToFit leaves right margin

I have a UITextField that I'm trying to center in a UIView. However, when I call sizeToFit it leaves a small right margin in the textField which causes the text to be slightly off center when the textField background is clear. I've colored it here so you can see what I'm talking about:
messageField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
messageField.delegate = self;
messageField.font = [UIFont systemFontOfSize:15];
messageField.textColor = [UIColor darkGrayColor];
messageField.textAlignment = NSTextAlignmentLeft;
messageField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"not fitting right" attributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
[messageField layoutSubviews];
[messageField sizeToFit];
messageField.backgroundColor = [UIColor lightGrayColor];
And the textfield looks like this (I would post a picture but need more reputation points, this is basically the same though):
not fitting right
Any help would be greatly appreciated!
I should not that if I create a UILabel of the same font size and text, call sizeToFit on the label and then assign the UITextField frame to be the same as the UILabel, it works like a charm.

Hot to get UILabel Width using AutoLayout?

I might missing something very trivial here, but I can't figure out how to get the width of my UILabel. Note that is it being added programatically, and the it's size fits the text inside it (the text vary from 2 to 35 characters). It automatically fits the right width of it's content, but I need to get the width.
My code to add it to the screen:
UILabel *textLabel = [[UILabel alloc] init];
textLabel.text = textInputFromUser;
textLabel.textColor = [UIColor whiteColor];
textLabel.backgroundColor = [UIColor colorWithRed:40.0/255.0 green:40.0/255.0 blue:40.0/255.0 alpha:0.95];
textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:textLabel];
Note that the following code (with "Bounds"/"Frame") always returns 0.00):
NSLog(#"textlabel width: %f", textLabel.bounds.size.width);
You can only find out the size after the first layout pass. So either wait for that or call
[textLabel layoutIfNeeded];
to layout it immediately. After that the frame should be set to the right value.
Since no one else answered, I'll throw in my two cents…
I assume the reason you're unable to get the frame of the label is because the frame hasn't technically been instantiated; but nonetheless, maybe just getting the size of the content would be sufficient for your needs.
Here's a post explaining how to use boundingRectWithSize:options:attributes:context: to get the size of the text in your label: https://stackoverflow.com/a/18961502/2274694
Try initializing the label with a dummy frame like so:
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1,1)];
textLabel.text = textInputFromUser;
[textLabel sizeToFit];
or you could try:
UILabel *textLabel = [[UILabel alloc] initWithString:textInputFromUser];
for swift I had to add following code block to get correct width for UILabel:
private var infoLabel = UILabel()
infoLabel.translatesAutoresizingMaskIntoConstraints = false
infoLabel.frame = .zero
infoLabel.font = UIFont.boldSystemFont(ofSize: 14)
infoLabel.numberOfLines = -1
infoLabel.text = "infoText"
infoLabel.sizeToFit()
to get correct width:
let infoWidth = infoLabel.frame.size.width
I also added correct constraints to my label (which is infoLabel) according to design given to me.

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

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.

Resources