UILabel set text vertically - ios

I have a UILabel that I want to expand vertically rather than horizontally after some maximumWidth. Currently I am trying to do this by doing the following:
_caption.numberOfLines = 0;
_caption.lineBreakMode = NSLineBreakByWordWrapping;
and then when I size I use sizeToFit
However, I always have a width that is much larger than my maximumWidth.
I tried using preferredMaxLayoutWidth but this did not work.
I could do a simple calculation by dividing the width by the maximumWidth that I want and then adjust the height accordingly but I'm wondering if there is any way to do this automatically.

You need to use NSLineBreakByCharWrappinginstead of NSLineBreakByWordWrapping
_caption.lineBreakMode = NSLineBreakByCharWrapping
I had tested this using a simple label and storyboard and setting the constraints to top, left and width and here is the result

Related

How to break line of a UILabel dynamically with text?

I have a UILabel and I want to show some text in this label. I want to increase the label width at most 70% of the full screen of my device. If text length of that label doesn't fit this 70% of size then the label automatically goes to the next line as long as the text length. Every time the label length cross the 70% width of main screen then lines break as well. I have tried several ways but unable to solve yet. Please help me to solve this.
Thanks in advance;
Drag a label to your storyboard and add top and leading constraints to it.
Now select the label and control drag to the view holding the label (in your case view of ViewController) you will see the pop up and then select equal width
Now your Label's width is equal to your view's width :) That's not you want you want ur label width to be 70% of your view. So select the equal constraint of label, go to property inspector and change the multiplier to 0.7
Now your label width is 70% of your view!
But you don't want it to be 70% always. It can be at max 70% of screen, so
now change the relationship of constraint from being equal to less than or equal to.
select label and change number of lines to 0.
That's it :) have fun :)
Sample O/P:
When text is short - vs - long:
- - -
EDIT:
Not using a storyboard? Not a problem; write the same constraint programmatically and apply it to label simple enough. If you need help lemme know :)
EDIT:
As you have specified that you want to leave the gap at the beginning of each line in label you can achieve it by using Edge insets
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
You must have forgotten to increase the label's height.
The code below is for allowing the UILabel to have multiple lines:
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
Then you have to make sure the UILabel's frame has enough height to show the lines. You can achieve this by calculating the required height for the given text (NSString):
NSString *text = #"YourText";
CGFloat your70Width; // whatever your width is
CGSize constraintSize = CGSizeMake(your70Width, MAXFLOAT);
UIFont *yourLabelFont; // whatever your font is
CGRect requiredFrame = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading attributes:#{NSFontAttributeName:yourLabelFont} context:nil];
// Keeps the old x,y coordinates and replaces only the width and height.
CGRect oldLabelFrame = label.frame;
label.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y, requiredFrame.size.width, requiredFrame.size.height);
Now the label will be shown nicely in multiple lines.
To increase the height of the label according to the content if you are using storyboard. Give the label FOUR constraints (Top, Bottom, Leading, Trailing) then go to Attribute Inspector and make lines to 0 and in line break do it WORD WRAP.

How can I set a multiline UILabel inside UITableCell on Swift 3?

I am trying to set a multiline UILabel, inside an UITableCell.
That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). The text inside of it can change so I need to fit on the UILabel being able to multiline when the width of the text is higher than the space of the UILabel width.
I tried using:
myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
myLabel.sizeToFit()
but it is always displayed on one line truncating the text that overflows the UILabel width.
How can I make my UILabel to be multiline?
EDIT: I cannot put breaklines to my text because I retrieve my texts from my database.
Thanks in advance!
These two lines are necessary for calculating the cell height automatically. Put these in viewDidLoad.
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
If you want a label to switch from one line to multiple lines, you either have to specify line breaks in the text of the string with \n or newline char, or you can set constraints for the width of the label, or you can set leading and trailing constraints for the label. If one of those methods is complete, the label will go to as many lines as it takes to hit the max number of lines or infinite if set to 0.
Finally I was able to make my multiline UILabel adding this code:
self.tableView.estimatedRowHeight = 50.0
and reducing the multiplier value. It seems that the text could not fit on the height of the UITableCell.

UILabel height calculation

I have UILabel that the text is dynamically change based on server data. Sometime the data is so long that make my UILabel become multiline. Is there any way to calculate the height of my UILabel?
You can use this :
var labelHeight : CGFloat
labelHeight = theLabel.boundingHeightForFixedWidth(theLabel.bounds.width)
Hope it help :)
If you are using Autolayout then no need to calculate height, just add leading,top and trailing constraint, set number of line to 0 for that label and line break mode to word-wrap, it will automatically update its height.
P.S. Add the bottom contraints too if there is any other control in nib after label.

How can I know the height of a superview after I change one of its subview like UILabel

I change the My UILabel's text and make it become multiple lines. I guess the the superview of this label will resize after that. But how to get the new frame of the superview as soon as I change the label.text?
CGSize size = labelSuperview.size;

How to adjust UIlabel

I read this answer in how to adjust the height of my label based on the text of the label:
Vertically align text to top within a UILabel
This is my code trying to set new Text and adjust the label at the same time.
But what I find out is the height of the label never get changed. It ways stays at the value I set in my xib file. I use tool, Spark to inspect the height of the label:
-(void) setNewText(NSString *)newText
{
self.myLabel.text = newText;
self.myLabel.numberOfLines = 0;
[self.myLabel sizeToFit];
[self setNeedsLayout];
}
Can you please tell me what's wrong with my code?
Thank you.
Updated:
I have tried #rdelmar suggestion. Set the width constraint for the label and num of line to 0. The height of the ui label does get adjusted. But there are extra spacing before and after the first/last line of the text.
Here is the screenshot I captured using Spark. The blue rectangle is the UILabel. As you can see, there are extra spacing before/after the first/last line. How can I remove those spacing?
You don't need to do anything in code to get your label to adjust its height. In the xib or storyboard, give the label constraints to set its position, and give it a fixed width constraint. Do not set its height. Set the numberOfLines to 0, and that's all you need to do. If you set the text with a string that's too long to fit on one line, the label will adjust its height automatically.

Resources