Screenshot of the issue:
self.label.text = #"回复111111111111111111111111111111111111111";
I wrote the code to set the text of label, but the label always has a line break right after the "回复". Can someone help me to get this text to display all in one line?
You can use the sizeToFit property of a UILabel to adjust the height as per the text. Once you set the text, you can simply call:
[yourLabel sizeToFit];
You can set UILabel like this :
this code will adjust your font size according to your label width.
label.adjustsFontSizeToFitWidth = true
Related
I have a label called ratingstext and I want its contents to fit into a single line of text by means of adjusting height in case of multiple lines. How do I achieve that in iOS Objective -C.
My label:
as you can see the label shows 200/20 and is called ratingstext label. Actually the value is 200/200 but since I have used
self.ratingsText.textContainer.maximumNumberOfLines = 1;
The last 0 value gets chopped off due to fixed label width. How do I auto adjust width or height to fit in the content in a single line?
My code is:
self.ratingsText = [[UITextView alloc]initWithFrame:self.frame];
self.ratingsText.editable = NO;
self.ratingsText.text = #"000/00";
self.ratingsText.textContainer.maximumNumberOfLines = 1;
self.ratingsText.font = [UIFont systemFontOfSize:self.ratingSize weight: UIFontWeightRegular];
[self.ratingsText sizeToFit];
self.ratingsText.backgroundColor = UIColor.clearColor;
[self addSubview:self.ratingsText];
If you are using a storyboard autoLayout will expand the frame to match the size of the text input. In the Attributes Inspector make the width textLabel "Relation" to "Less Than or Equal". I gave the text label in the storyboard 200/20 and in code set it to 200/200. Auto Layout took care of the rest.
Use the below properties:
adjustsFontSizeToFitWidth
contentScaleFactor
minimumFontSize
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.
I am getting a weird behaviour of UILabel in ScrollView. I am assigning dynamic text to the UILabel and using sizeToFit, I can log the new size which seems to change correctly after assigning text but I can't see the changed size reflected in both the simulator and the real device. This is the log before assigning and after assigning text
--txt Frame {{15, 325}, {291, 36}}
--txt Frame1 {{15, 296}, {291, 446.5}}
As you can see, the size (height) changes though I don't see the changes on device and simulator. Please help.
try to add this after sizeToFit()
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
override func viewWillLayoutSubviews() {
self.myLabel.layoutIfNeeded()
}
Have you set the numberOfLines property for that label Before use the sizeToFit method.
may be that also cause the problem.
may be the label frame is change but label's numberOfLines property is set to 1 so it display only 1 line.
try to set the number of lines property before using sizeToFit method, like
label.numberOfLines = 0;
[label sizeToFit];
this will set the all the text for any number of line in label.
Hope it will Help you.
After experimenting with different approaches, I ended up manually extending the height of the contentView in IB myself. Its not the most elegant solution but it works for now :(
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.
I'm trying to show a long chunk of text inside a UILabel in one line.
The UILabel is a subview of UIScrollView so I can scroll and see the entire UILabel.
My problem is that the sizeToFit method only partialy works.
textLabel.attributedText = attributedString;
textLabel.numberOfLines = 1;
[textLabel sizeToFit];
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);
The UIScrollView content size gets big enough to show the entire UILable, but for a line like:
so i'll try to share some of them here every once in a while."
The UILabel shows:
so i'll try to share som...
What am I doing wrong?
Turns out the code is just fine - but the Use Autolayout was checked.
Unchecked it - everything works just great...
If you want to achieve this with auto layout turned on it's simple. Just make sure you add numberOfLines
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 0;
Surprisingly, if you did not put a constraint on the label's width, this would work:
[textLabel.superview layoutSubviews];
I learned this by trial and error.
try
textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.minimumFontScale = 0.5;
Since you have restricted your Label to show only one line of Text and truncate the rest , it is behaving the same
textLabel.attributedText = attributedString;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);
Hope it will help you
The most common reason for sizeToFit not working properly is the UILabel not having any autolayout constraints, for instance if you're implicitly relying on the view position remaining fixed relative to the top left. Adding any constraint at all (leading, top, centerY, anything) will fix it, presumably because it will result in layoutSubviews being called at some point, as suggested in Maxthon Chan's answer.