Attributed label with custom line height cropping text on top - ios

I need to decrease line spacing on interface builder using attributed text, and changing the line height multiplier it works as desired, but the text is being cropped on top.
I already tried to set the lineSpacing with NSMutableParagraphStyle by code but did not worked or happened the same thing.
There is a way to fix the alignment or setting the correctly the line spacing?

So it isn't a matter of spacing, but more likely constraints. Perhaps your baseline setting is set to none, in which case try changing it to 'Align Centers'.

Related

UILabel height depending on font size

I am using a UILabel in a UITableViewCell to display a title. Normally the text is not too long and therefore the font size can be large. Notice how the text fills the height of theUILabel like normal.
However, when there is a larger title, I want the UILabel height to reduce to accommodate the smaller font size and not leave a blank gap in its place. Currently, my configuration produces this effect.
I am using constraints in my storyboard and have deliberately not set a fixed height constraint. Here are the relevant constraints added:
I may have made a rookie error in my configuration as I can't remember this effect happening before, but it is giving me real headaches (and a bad looking UI).
When UILabel is set to auto-adjust font size to fit, it does NOT auto-adjust the height of itself.
You have a couple options:
set Baseline to Align Centers, and just accept that you will have top and bottom padding
use code to calculate the "scaled font size" and update the font size of the label
Remove either the Top Space or Bottom Space constraints (depending on where you want the label to be anchored).
This will cause the label to automatically use fit itself to the text size.
Try this and see, it should work according to your need:
Set Top and Bottom constraints attribute Greater Than or Equal To and add horizontal center (Alight Center Y to superview) and show in this image.

iOS10 UILabel use NSLineBreakByCharWrapping has half character ,how to fix it?

this is UILabel show half character!!!
this is UILabel attribute!!!
The Linebreaking is only working if the label have the chance to break the line. But with "Lines" set to 1, there is no chance.
Set the Lines to 0 (auto) or at least to 2 and it will work.
just set auto shrink to minimum font scale from storyboard so it will automatically set the size of font according to the label width or programmatically set [self.yourlablename sizToFit]

UILabel AutoShrink property in not working in ios 7

I added Autoshrink Property of my label from nib file as follow;
It is working good in ios6 but not effected in ios7.
ios6:
ios7
Why this happen? I am using numberoflines=2
Help to solve this
Thank you
Yes that is because you are using
numberOfLines = 2
that means your label will be of two lines, so its auto adjusting the size accordingly as your label will be of 2 lines,
set background color of the Label to get to know about the label dimension.
EDIT -
If you want your labeltext to fit someother Superview element accordingly and have thinking that your label can have a text too long, then set numberOfLines = 0.
Basically numberOfLines means the Maximum line breaks the text of the label can have
Set your FONT to a lower value - 14,13 (judging by your previous one that is what i am sensing)
Set your Label to a fixed width, so that you know that a nextLine would appear after that width,
Set Number of lines to 0

Allow UILabel to grow dynamically using auto layout

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Does an NSAttributedString in a UILabel ignore the label’s numberOfLines property?

I need to increase the line spacing in a UILabel, so I’m using its attributedString to do so. Works fine, but it ignores the fact that I had also set the label’s numberOfLines to 2 – it only displays a single line, and truncates.
Any idea why this might be, what I can do about it, to still use two lines, but increase the spacing between them?
It sounds like the frame of your UILabel isn't big enough. Try increasing the height and see if the problem persists.
You can force the label to the correct size for your number of lines by calling sizeToFit after setting the attributedText. To avoid the problem of the shrinking label in your table view cells, reset the frame of the label to some default size before setting the attributedText and calling sizeToFit. This will prevent the label from continuously shrinking.

Resources