I have a UILabel which is by default showing only 3 lines of text. I want to display all text content in some cases. I have tried everything I found, but nothing works. Here is my latest try:
CGRect labelSize = [contents.contentLanguage.Description boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
cell.descLbl.frame = CGRectMake(labelSize.origin.x,
labelSize.origin.y,
ceil(labelSize.size.width),
ceil(labelSize.size.height));
[cell.descLbl setNumberOfLines:0];
[cell.descLbl setLineBreakMode:NSLineBreakByWordWrapping];
[cell.descLbl sizeToFit];
Maybe you should look at maybe using the UITextView instead? It's much better suited for multiline text.
https://developer.apple.com/documentation/uikit/uitextview
Ok, so I was able to resolve this issue by changing the height of the UILabel from the .xib file to >=0
Please use UiTextView in place of UILabel
textviewobj.text=#"";
[textviewobj sizeToFit];
You have two ways (choices) to do it:
Use UITextView without editing permission
Use UILabel with numberOfLines = 0
Use UITextView without editing permission
If you've fixed space (limited area to display your content i.e. fixed height) to diplay your content and you can't work with scrollable content then you should use UITextView without editing permission.
Use UILabel with numberOfLines = 0
If you've flexible space (no limited space/area to display a your content i.e. fixed height) to display your content, then you should use UILabel with number of line = 0.
Do not assign height value (constraint) if you want a flexible UILabel with height, that fits according to size of your content.
Or Assign minimum height value (constraint like >= 30) if you want to have/set a minimum area for content, whether there is no content to be display.
Related
I see that UILabel in IOS has height property along with font . I also see that if I give the height and font same value, for some characters the text gets cut in UILabel. How are these two different? While doing the UI design if height is more than the font size, there would be some extra white space which is what I want to avoid and hence wanted to know what exactly is the difference between the two.
You can use sizeThatFits: to determine correct height for UILabel
UILabel *label;
label.text = #"Some text";
CGRect labelFrame = label.frame;
labelFrame.size.height = [label sizeThatFits:CGSizeMake(labelFrame.size.width), MAXFLOAT].height;
label.frame = labelFrame;
The text size is the height from the font's baseline to its cap line, which doesn't take into consideration descenders (like on the letters 'g' and 'y') or ascenders (like 'f', in some fonts). In general, you don't have to (or even need to) set a label's height; it determines the best height based on its font size.
See https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html for details about how font sizes work.
I am finding it surprisingly hard to resize a label containing newlines based on the quantity of lines and text. It displays fine in a large enough textview. However, I'd like the economy of sizing the label--or I'd be happy with resizing a textview--exactly.
This is the code I am using from an answer on SO but it is having no effect on the size of the label. Would appreciate any suggestions on how to make this work:
NSString *list = self.list.list;
// use font information from the UILabel to calculate the size
UITextView *view=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 10)];
//make random size view
view.text=list;
CGSize size=[view sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)];
// create a frame that is filled with the UILabel frame data
CGRect newFrame = _listLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = size.height;
// put calculated frame into UILabel frame
_listLabel.frame = newFrame;
Why are you setting the frame of your label with reference of a newly created UITextView, it will create a useless object in your memory, to set the label frame according to your text just use this 2 line of code
lbl.numberOfLines=0;
[lbl sizeToFit];
It will make the label as large as your text.
You really should use autolayout.
Just constrain the label where you need and let UIKit do it's job.
Here an example:
I set a top space and a leading margin constraints
Then I added a width constraint and then I added some more text
As you can see the label resized itself as it knows how much text it has inside and how much space it occupies.
There are a UILabel in the center of the UIView, named nicknameLabel, and will append a gender image after the nicknameLabel.
But when the nicknameLabel has a lot of text, it will beyond the bounds of the UIView.
So, how to limit max width of UILabel?
BTW: I using the storyboard. Thanks.
Give the label a <= constraint like this:
use following code and set your width in boundingRectWithSize value.
CGSize itemTextSize = [#"Your Text" boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName : [UIFont fontWithName:#"Helvetica Neue" size:12.5]} context:nil].size;
If you are using constraints then remove all constraints from you label.
Then again set frames of your label and if you want to make multiline label, then simply set the number of lines from 1 to 0 and increase the height of your label.
OR
If you want to fit your text within the label.You can simply enable Autoshrink property.
I had a look on SO before asking this question. All the questions are about adjusting the height of a UILabel and not its width. I tried alternative methods but it did not work such as [label sizeToFit];. Is it possible to adjust the width of a label based on its text? I create my label in a UITableViewCell in story board. I want to be able to adjust its width based on the text that it is assigned. I dont want to resize the font size.
I set the text of the label in CellForRowAtIndexPath.
Examples would be great please.
Thanks in advance :)
Update 1: I have a custom cell that I am making in Storyboard so not programmatically. I set the contents of each cell in CellForRowAtIndexPath, for example, myLabel.text = recipe.name. The name label is quite small, however I would like to extend its width based on the length of the text, not truncate the tail or shrink the size of the text.
Update2: I have a lot of other UIElements in the cell. So I have a label in the top left, top right, bottom left, bottom right, and a picture in the middle, there default is 120 because they have a background color. I set it small the there is not a huge amount of empty space in the label.
Get the size of the string:
//Replace FLT_MAX with the maximum height/width you want the label to be, if no maximum leave as FLT_MAX.
CGSize stringSize = [YOUR_STRING sizeWithFont:YOUR_FONT constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
Then size your label:
[YOUR_LABEL setFrame:CGRectMake(0, 0, stringSize.width, stringSize.height)];
In iOS 7 sizeWithFont is deprecated, use boundingRectWithSize instead:
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:YOUR_LABELS_FONT
forKey: NSFontAttributeName];
CGSize stringSize = [text boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
CGSize maxSize = CGSizeMake(250, CGFLOAT_MAX); //250 is max desired width
CGSize textSize = [Label.text sizeWithFont:Label.font constrainedToSize:maxSize];
Label.frame = CGRectMake(0, 0, textSize.width, 15); // desired Bounds
Using above code you can get frame size for perticular text, & then resize label accordingly
You don't need to set explicit width for UILabel. UILabel provides intrinsic content size when used with autolayout. If you add constraints that provide the label's x,y position(Top Space constraint + Leading Space constraint), autolayout will be able to determine its width height based on the content.
I have a UITextView with Content and frame size widths equal to 348, but word wrapping happens whenever text width on a line exceeds 338.61. Does anyone know why this might be happening? How can I access the width that the UITextView uses for word wrapping dynamically?
As of iOS 7, yes. It's a combination of textContainerInset and lineFragmentPadding.
UITextView *textView = ...;
CGFloat wrappingWidth = textView.bounds.size.width - (textView.textContainerInset.left + textView.textContainerInset.right + 2 * textView.textContainer.lineFragmentPadding);
Which is a nice value to know about. You can use it in boundingRectWithSize: calls, for example:
CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(wrappingWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin // this is important
attributes:#{ NSFontAttributeName: textView.font } // and any other attributes of your text
context:nil];
Also cool is the fact that you can set textContainerInset and lineFragmentPadding, so if you want to increase this or have a UITextView that renders with no insets (so it matches a UILabel, for example), you can.
Nothing wrong with the answer above but I have been researching the UITextView word wrapping behavior and have discovered that although one can adjust the contentSize of the textview and the corresponding size of the textContainer, one additional method of changing the text wrap position (when using Auto Layout) is to make the trailing space to the SuperView into a negative value. This will allow one to push the word wrapping feature outward to the right. Not finished with the research just yet, but this tip should help anyone seeking to better control the UITextView word wrapping behavior.
Will post code once I'm complete but for now adjusting the trailing constraint of a UITextView will move the rightmost position of the word/character wrap.