ios. UILabel resize issue - ios

I have some issue with labels layout using storyboard.
Left view snapped to labels trailing constraint, and centered according to text, and nothing more. So problem appears when I try to set different text to label, (when text contains number of characters greater then in text on story board). After text has been setting label does't resize it's frame.
Here, how it looks in runtime:
I have the same issue with other all labels in project, they are don't resize in runtime, but They should. Here is how it should looks like :
I'v already tried to add layoutIfNeeded() and sizeToFit() to those labels, but it has no effect. I made another project, with pretty same label and constraints, and there it works fine, as should. But not in this project.
What problem could be in?

It could also depend on when you're setting the text of the label.
If you're setting the text in viewDidLoad try doing it in viewDidAppear.

Related

Wrapping text in UITextView in Swift

UPDATE: Looks like the UITextView's width is bigger than screen's width.
It is strange since I created the UITextView using a storyboard and in preview mode it looks like it's width is less than that of the UIViewController.
I have a UITextView, in a UIViewController, that was created using storyboard.
I have similar code to populate the text in the UITextView.
self.taskGroupDescriptionTextView.text = taskGroup.description
self.taskGroupDescriptionTextView.textContainer.lineBreakMode = NSLineBreakMode.ByCharWrapping
I tried different types of lineBreakMode, break by char/word. None of them work.
My UIViewController when run on iOS simulator still has text that extends beyond the screen.
You should set constraints. Here's an example how to do it:
You click on your UITextView so that you can start editing it. At the bottom of the storyboard there are few icons and clicking on the one shown in the image will open up an editor where you can add constraints. You have plenty of options (pin to left, right, top, bottom, fix height or width, aspect ratio,... and even more if you click on a neighbour icon). Keep in mind that you have to set enough constraint so that it will be reasonable for your view controller to calculate the frame size of your view. But no worries, if you'll forget something you'll get an error.

text over an image in the cell

I have a text that should be placed over the image. Now the image covers the text, any text that is not visible. I thought here is what the priorities for the display, but did not find them. Tell me how you can specify that the text should be drawn over the picture, and not vice versa.
debug view
The order that things appear in the navigation bar (the second screenshot) is the order they draw in. The lower down they are the later they are drawn. So at the moment your image view will be behind you text (and everything else).
That's actually what it shows in the Interface Builder also as you can see the text.
TBH, your AutoLayout constraints look a little suspect to me. I suspect they are pushing the label off the screen. I really don't think you want the constraints that way. You don't need a height to begin with and if you want the label centred then give it a centre constraint not a leading constraint.
What, exactly, is giving you the impression that the image is over your text? Have you tried the view debugger in the simulator?
You can call bringSubviewToFront method in code [self.view bringSubviewToFront:self.label];
I found solution - mode = Aspect Fit

Xcode Label with long Text not properly shown

This is my first IOS app, just for training, and I got some issue with Label ,check the image
As you can See Object: Photo- Visualizing the Thomas Walther is not properly shown and some words are missing.
So How to fix it and make the string appears on multiLines if it's too long?
In your storyboard, select the UILabel and open the Attributes Inspector (in the utilities, which can be opened with the shortcut opt-command-4). You can change all the the basic properties of the UILabel here.
For multiple lines, ensure that the label height is large enough and set the Lines to 0. You can also change the font scaling in the Autoshrink property (default is fixed font size).
I can't really tell from your screenshot, but you may have some Auto Layout issues with the width of your label to the right side of the window. So you may want to look into setting Auto Layout constraints as well.

Custom UITableViewCell - UILabel not aligned properly

I am creating a custom UITableViewCell using the Storyboard editor. I've got several components in this cell, but the biggest is a UILabel that is displayed at the top. This label should be left-aligned, however I'm finding that when I run the app in the simulator to test, the text is centered. I've tried specifying the alignment manually, but it always centers itself.
Is there something I need to do to get this working properly in a custom UITableViewCell? I've never encountered anything like this before.
Try using Debug->Color Render layers in the simulator to see if your text is actually centered, or the UILabel moved. Try removing the outlet if any, to prevent anything from tampering with it and see if it helps diagnose.
If the colored layer is offset, make sure you added proper constraints to the label (or autoresizing masks). Anchor it to at least left and top and leave the other 2 sides free.

Where is this vertical spacing coming from in UILabelView?

I'm creating an iOS view that displays various static text elements. The xib looks like this:
It uses four labels for the title, timestamp, body, and footer. Every view is anchored to the sibling view above it vertically and anchored to the left/right of the parent view. All labels have a fixed height except the body which has a >= height and the number of lines set to 0 with "word wrap" as the line wrapping style. The parent view is a UIScrollView.
On the iPhone it looks like fine:
However on the iPad it looks like this:
Huh? Where is all that extra vertical space in the body label coming from? The xib and its view controller are identical between iPhone and iPad (there is no custom iPad code at the moment). I've found that the vertical space is directly related to how many line-wraps the label renders. If no lines wrap, no extra vertical space. If only a few lines wrap, there's a little extra vertical space. If nearly every line wraps, well, that's what it looks like.
First of all any ideas on why UILabel is behaving this way?
Second of all, if I can't make it stop doing this how can I work around it?
I've already tried a few things. If I call [bodyLabel sizeToFit] within -viewDidLayoutSubViews then it fixes the label but doesn't fix the layout of any of the sibling views (e.g. the Footer label is stuck way at the bottom of the screen instead of pulled up to just under the body). Any attempts to get the entire view to re-layout its children after calling sizeToFit is ignored. I've also tried sizing the UILabel by calculating height based on font, which results in the same behavior as -sizeToFit (albeit with more code).
Replacing the Body UILabel with a UITextView instead doesn't give me the weird vertical spacing issues but I need to calculate the height of the UITextView manually (using font calculations) and something about resizing the UITextView within the parent UIScrollView makes it so the UIScrollView simply refuses to scroll (as if it doesn't know its contents are too big for its bounds).
So at the moment I'm stuck. Even just an explanation of why UILabel behaves this way on the iPad layout would be helpful.
In case anyone else runs into this same issue using autolayout... I may have been able to solve the same issue by creating a constraint as Coche suggests, but I realized I had a preferredMaxLayoutWidth that was too small set on the uilabel. Once I set an accurate preferredMaxLayoutWidth (the actual width of the label) the spacing on top and bottom disappeared.
The main problem is that the method for auto resizing the text inside your Label is failing because in iPad your Label doesn't have a set width from the beginning, it is calculated on run time and that's the source of that mess. On iPhone, as your Label has a set width (on IB) there is no troubles.
There are two ways for solving the problem:
Having two storyboards : one for iPhone and one for iPad
Doing this will make that your Label knows its width since the beginning and it will just works as on iPhone.
Having just one Storyboard for both iPhone and iPad
You can go around the problem by calculating the size that best fits its text and with that result add a height constraint by code to the Label. For calculating the desiredSize you can calculate the width with this formula: Current View's width - (Leading space + Trailing Space). Here is my code
CGSize desiredSize = [_bodyLabel sizeThatFits:CGSizeMake(self.view.frame.size.width-40, 10)];
NSString *visualContraint = [NSString stringWithFormat:#"V:[_bodyLabel(%.0f)]",desiredSize.height];
[_bodyLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualContraint
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_bodyLabel)]];
objective-c

Resources