iOS multiline label not gong to multi-line in Xcode 7.3 - ios

I'm trying to set this up and have always done this in code and would like to get this done in modern version of Xcode 7.3.
I do the following steps:
drag out a UILabel
set the number of lines to 0
set Line Breaks to Word Wrap
But this isn't breaking and representing all the data. The data is being populated though. What else do I need to do?
edit 2
added border for this

The problem is that when you do this in code, your label is not using auto layout. But when you drag into the storyboard, your label is using auto layout, so the rules are different. You need to add constraints.
What you do now depends on what you want. First you'll need constraints for top and left of the label. Then, if you add constraints for just width, the label will grow downwards as it fills with text. If you add constraints for both height and width, the label will simply fill up within that size.

Related

minimum number of lines UILabel in iOS development

I was trying to make a cell in uicollectionview. please see these two images
Thats the cell of a collectionview and the first uilabel's number of line is 2. what I am trying to do is the uilabel will always take 2 lines of height regardless of the content of the uilabel, is it possible? if the text is short, second line will be blank.
If I add height constraint, the text is vertically centred. How to make it top-aligned?
There are 2 steps.
First select your label, click the add new constraints, and check the height constraints, as grow4gaurav said.
Next, go to the attributes inspector and set the number of lines to 0. This makes it so that the text uses as many lines as it wants. So, if the text is short, and it only uses one line, it will just use the top line. If it is longer, it will use the bottom line too.
Hope this helps

AutoLayout to dynamically size UILabel Height and Width in Map callout?

I have to populate data on 2 UILabels located in .xib. This is used for Google Map SDK iOS for a custom callout (in map annotation). How do I set constraints in interface builder using Auto-layout ?
Here is what I have for now. I need to make height and width dynamic. Thus some callout does not include arrow icon.
How each label attributes looks like:
Its almost there, what happening is container view doesn't expand/shrink according to description label.
What are the steps I should follow to build this view from scratch (for the learning purpose as well) ? Plus different ways this can be achieved?
Just change number of lines to "0" for label, auto layout will handle rest of the things. Remove height constraint because the height of the label will be determined by font size and number of lines of text.
.

UILabel adds unnecessary top and bottom padding when growing vertically and breaking lines

Let's say we have a UILabel, that is pinned from all sides to its superview. Number of Lines is set to 0 (so we can display multiline strings) and we use Truncate Tails strategy for line breaks.
If we have a single, short line of text, which does not break lines, the label's inside padding/inset looks fine:
When using multiline text, that will force the label to start breaking lines, ellipsis appear at the end of the label, but artificial padding/inset is added to top/bottom of the label as well. The label is inflated vertically, even though it is still displaying only 1 line of text:
Constraints:
Line breaks:
What I tried already
Changed Content Hugging Priority to 1000 for both Horizontal and Vertical axis, so the UILabel doesn't grow more than required, but that does not solve the issue.
Switching between different Line Breaks strategies (Clip, Character Wrap, Truncate, ...) does not solve the issue either.
I am able to reproduce this behavior in both Xcode 6 and Xcode 7 beta, Interface Builder and while running the app.
Looks like the only way to fix the unintentional padding is to set Lines to a constant (eg. 3). The UILabel will still grow dynamically and will refrain from adding any padding.
Unchecking the 'explicit' checkbox in Interface builder for the Desired width of the label solved this for me.
I got the same problem when see it in interface builder. But if i just run it the final result in simulator works without any padding.
The key is you have to set the backgroundColor property of the label.

Adding multiple UILabels in view with constant height between them

I am trying to add some 5-6 UILabel's (Single line only) in a UIView using auto layouts. I have added constraints for top most & bottom most UILabel. The problem is variable screen sizes.
Just wants to know a better approach of adding those remaing (3-4) middle UILables (As I want to show equal space between all the UILabels).
Approach 1 - Add height constarint for middle lables & based on screen size change the height constraint constat at runtime.
Approach 2 - I tried adding >= and <= constarints but I am not getting the output as desired.
Any help, how can I keep constant height between all UILabels.
(Can be achieved via UIScrollView or UITableView but I dont want scrolling .. only using auto layouts:))
You can add dummy UIViews between your UILabels and set those dummy views' heights to proportional to their container.
Or you should be able to achieve that by setting your current constraints constants at runtime and then calling setNeedsLayout&layoutIfNeeded
You can't achieve that by playing with "less than" or "greater than" constraints.
Edit: You can also use UILayoutGuide to do that on iOS9.0+
You can use stack view from left window where you have button/label/view etc..... or also you can add it from constraints tools(see attached image)
If you are looking for the attached solution, please add below constraints shown in the third image.

String with new line character ios7

I'm very new to iPhone programming. actually i've started to write a small app and I'm trying to set a text that contains a new line character to a label.
I have set the label properties to 0 number of lines and selected word wrap.
my text looks like this : #"ABCD\nEFGH\nIJK"
instead of printing on a new line on the label it truncates the string after the first new line character. any ideas?
If auto layout is on, and you don't add any explicit constraints, the system adds them for you. If you look at the size inspector when you haven't added ay constraints yourself, it says this,
The selected views have no constraints. At build time, explicit left,
top, width, and height constraints will be generated for the view.
Having that height constraint is what's probably causing your problem. Add constraints to position the view horizontally and vertically, but don't add any width or height constraints, and that should solve your problem. The system will adjust the width of the label to match your longest line.

Resources