iOS Autoshrink not working on single word in UILabel - ios

Using autolayout to have a multiline UILabel auto size the text to fit does not work for single words.
Here is how storyboard is set up:
Here's the problem.
This does not display properly:
But this does:
I have adjusted every setting in the storyboard. I have changed all of the Line Break settings (wrap, truncate, etc), I have adjusted # of lines, I have made text plain vs attributed, I've changed font sizing and scaling, everything. What am I missing? Why does "California" get cut off instead of shrinking to fit? I've seen many other posts on SO and haven't yet found a solution (which needs to be compatible back to iOS 9)

Your label is being filled both horizontally and vertically until it runs out of space, then it starts shrinking the text to fit. In the one word example it breaks the word into two lines because there is vertical room.
Do you know in advance or can you calculate in the app when you have only one word in this label? If so you can change the number of lines to 1 instead of 0 so the app will keep the contents on one line and shrink this word.

Related

how to set which label should be truncated if labels overlap?

i have a similiar problem as asked in How to resolve the Storyboard's warnings on Xcode?
However my left label can get really wide at runtime and the standard behaviour is, that the right label is truncated. How can i force the interface builder to truncate the left label and not the right one?
Both texts in the labels are set on runtime, so i have no knowledge of the resulting width of each label.
As stated above i used the solution from the linked question but the wrong label is truncated. So my problem differs from the one stated in the link.
There is a property called content compression resistance.
This determines how hard it resists compression of content.
So... for the label that you want to keep full length run this...
label.setContentCompressionResisyancePriority(.required, axis: .horizontal)
I’m typing on my phone so I may have messed up the parameter names. But auto complete will help.
This will make sure that the other label is truncated first.
You can also set this in IB in the measurements panel near the bottom.

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.

Autoshrink in UILabel only for width?

I have some ASCII syntax diagrams which must not have line breaks in the middle.
These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle.
I just want to shrink only if the content doesn't fit the width. It would be absolutely fine to scroll vertically through the text.
What is the best way to do this with UILabel or any other UI element?
Use UITextView with 'editable' property set to false.
So let me rephrase your question. I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. If this is what you want, well the imagination is weird to me...
But anyway, I feel there's a conflict in your settings. First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. But then Autoshrink will play no effect. I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code.
I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. I guess you want to take a look about this:
Autoshrink on a UILabel with multiple lines
The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. Maybe this can achieve what you want.

Xcode label causes line break in middle of word

I've been working with a label on a storyboard and can't seem to get it to behave correctly. I have my lines set to 0 because I display a couple different things in the label, depending on what the user clicked before. 3 or 4 words will display fine and on multiple lines, but when I get to a larger single word it will break the word in half. I don't won't it to do this, I just want it to resize and stay on a single line if it's only one word. Any ideas how to do this? I already have it working so that it will auto resize long text to fit there, the only problem is this.
If a UILabel is too small in its width, it cuts off the last part of the text or breaks its content into another line. As one word does not break into half automatically it can't be displayed entirely and therefore is cut off at its end.
In order to solve your problem I have some suggestions:
Change the number of lines to at least 1
Update your frames
Resize the UILabel so it can display the entire word.
Take a look at your constraints, as they might force the UILabel to resize under certain conditions.
I hope this might help you.

UITextView word wrap breaks in the middle of a word

I'm using a UITextView to present definitions which may be one or more lines long. When it does go past a lane, if often wraps in the middle of a word.
Is this normal? What can be done to prevent this?
word wrap IS the default for IOS UITextView and the only reasons it fails Quuxplusone mentioned in his comment.
EITHER
the one word is longer (in pixels) than the textView's frame
the 'spaces' aren't really spaces but unbreakable spaces
It appears that this can occur when exclusion paths are too close to the edge of the UITextView. In this example accompanying this tutorial you'll notice that if you run the app and select the Interaction tab that while the oval is placed centrally words hyphenate and break naturally, if you drag it to the left edge then words break forcibly and are fragmented. But if you drag the oval half off the screen then wrapping works.
So when working with ovals you can use the trick of going off the left edge, and with rectangles, just don't get too close to the edge. Being up against it is fine.
You can do it as :
UITextview having a property of enable scrolling. So the text characters limit doesn't matters. If there is more text, the textview make it scrollable.
OR
Make the textview height dynamic, get the string/text height and set the height of textview accordingly.

Resources