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.
Related
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.
I want to display a switch button and a text on the same line. (and a list of those)
But when the text is too long, it doesn't spread on 2 lines right away, it first takes over the switch. Then goes on 2 lines if even longer. I, of course, cannot manage the length this string will be.
My constraint right now is very basic : #"H:|-[switchBtn%d]-[nameL%d]-|"
And the top view is simply #"H:|-[containerView]-|"
I tried using a size that was about 70% of the screen width with the metrics, but when I'm rotating the screen, I can't make it update this value correctly. It will not resize when changing screen orientation. I've tried updating the metrics dictionary, and re-attaching the constraints/updating them. No can do.
Is there something I'm missing somewhere?
Is it a bad idea to change the metric dictionary?
[nameL setContentCompressionResistancePriority: UILayoutPriorityDefaultLow forAxis: UILayoutConstraintAxisHorizontal];
Did the trick. Thanks Guy.
I have a view which has a lot of labels. Some of them get the text set dynamically. Sometimes the text is to long to display it within one line. I know how i can display the text in multiple lines. My problem is that when i do display the text in multiple lines then the margins are broken between the labels. Sometimes a label even swapes into the content of the next label.
I want to avoid fixing this "by hand" (repositioning of all other elements). Maybe Autolayout can fix this, but sadly i'm not able to use it for now. But it would be a good argument to convert my project from springs&struts into autolayout. However, maybe there is another way to fix this issue. Would be nice if somebody can help me out. Thanks in advance!
You can use sizeWithFont:constrainedToSize:lineBreakMode: to calculate the height which will be required by each of the labels. You should use this in a loop which iterates over each label in the order they should appear on screen. As you go, increment the y position by the height of the current label and the margin. Each label will now have the correct position and at the end of the loopy will hold the full required height.
I have a problem with label and I don't understand where is the solution.
I set my label with "Word Wrap" and 3 lines but it cut a word when text wrap...why??
thanks
Probably bounds are too small? Stretch the UILabel out as much as possible (height-wise), set lines to 999 and see if it still cuts it.
edit: Somehow this still gets views. More recent versions of iOS no longer have as many issues with UILabel, so this no longer applies. (especially with Auto Layout)
This is a really common problem when trying to use multi-line UILabels. The short answer is to use a UITextField instead if you want multiple lines of text. You get more control over the padding around the text as a bonus.
(yes, you can 'fix' the UILabel but sometimes simpler solutions are better)
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.