Label height stays the same with boundingRectWithSize. iOS - ios

Basically, I am trying to change my label's height like a typical chat bubble would. I have the following code in cellForRowIndexPath:
CGSize maximumLabelSize = CGSizeMake(296,9999);
expectedLabelSize = [cell.myMessageLabel.text boundingRectWithSize:maximumLabelSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
NSLog(#"%f",expectedLabelSize.size.height);
NSLog(#"%f",cell.myMessageLabel.frame.size.height);
//adjust the label the the new height.
CGRect newFrame = cell.myMessageLabel.frame;
newFrame.size.height = expectedLabelSize.size.height;
cell.myMessageLabel.frame = newFrame;
NSLog(#"%f",cell.myMessageLabel.frame.size.height);
With my NSLogs, it tells me that the height has been changed. However, when I run the app, the labels in my cells remain the same height. How can I make it so that it loads the correct height the first time the view is loaded.

You can try this.
cell.myMessageLabel.translatesAutoresizingMaskIntoConstraints = YES;

Related

How to get a height a UITextView should be given width and length of text?

I am adding a UIView that contains a UITextView which is constrained to the top, left, and bottom of the view.
The width of the UIView should be the screen size's width which in turn will be the UITextView's width.
When I go to create this UIView, I can make it's CGSize to the screen size's width, but I am not sure on how to calculate the height.
How can I figure out what height I must set this UIView so that the UITextView can properly show? Is there a way I can figure out the number of lines a UITextView would have given a certain width?
You can use the following code to Get a dynamic Rect:
NSString *myDynamicString = #"Hello World!";
CGSize textRect = [myDynamicString boundingRectWithSize:CGSizeMake(MAX_WIDTH, MAX_HEIGHT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont fontWithName:#"YouFontName" size:15]}
context:nil];
Note
MAX_WIDTH Width allowed to expand the rect. If you give 200 then the rect will exapand till 200 and then break and Vice Versa for MAX_HEIGHT
Use this code
CGSize constraintSize = CGSizeMake(textView.width - 2*textView.textContainer.lineFragmentPadding, CGFLOAT_MAX);
CGFloat messageTextViewHeight = [text boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName: textView.font}
context:nil].size.height;
-(CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize
{
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
First of all if you are using auto-layout this will be done out of the box, you don't have to do anything just setup constraints of auto-layout properly.
NSSString API is not very handy (as you can see in other answers) it is easy to miss something. I prfer use -(CGSize)sizeThatFits: from UIView.
UIView *textView = self.textView;
CGSize fitSize = { CGRectGetWidth(textView.frame), CGFLOAT_MAX };
CGSize fitSize = [textView sizeThatFits: fitSize];
NSLog(#"Your desired height: %f", fitSize.height);
It is more reliable and it should take into account all properties of text inside UITextView. I used this for UILabel didn't test for UITextView but it should work nicely.
If you want to see all the text in the text view an have the surrounding views around it adapt to contain it the easiest way would be to use a UILabel instead. Then you could just use constraints with priorities. Set the UILabel number of lines to 0 so that it automatically wraps. Then set the contentCompressionResistancePriority to 1000 for vertical axis. Then you don't need to worry about the sizes. Let autolayout do the work for you.

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

Set proper gap between text.view frame and button frame

I have a text view and buttons. For some reason i got unexpected result when trying to set proper gap between those 2 elements. I think there could be incorrect calculating of text.view frame. That is how i manage to set text.view frame:
//1 Setting properties for text, starting with dictionary containing font size and family
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"helvetica neue"
size:14]
forKey: NSFontAttributeName];
//Creating frame to hold textView
CGSize maximumLabelSize = CGSizeMake(self.myTextView.frame.size.width, FLT_MAX);
CGSize textViewSize = [self.descriptionStringShort boundingRectWithSize:maximumLabelSize
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
//Setting frame
CGRect frame = self.myTextView.frame;
frame.size.height = textViewSize.height;
NSLog(#"%f height", frame.size.height);
self.myTextView.frame = frame;
After that, when tried to set button frames it always show different gap between those to elements (my "rough" solutions is to check height of text.view and set "minimum" and "maximum" y depend on it, but it won't work in a real world app). For example, button frame may be draw on textView.frame or much below it. There is how i set map frame:
CGRect mapButtonFrame = self.mapButton.frame;
mapButtonFrame.origin.x = 30;
if (frame.size.height < 50){
mapButtonFrame.origin.y = (200+frame.size.height + 150);
} else if (frame.size.height >300){
mapButtonFrame.origin.y = (200+frame.size.height +50);
} else {
mapButtonFrame.origin.y = (200+frame.size.height +100);
}
self.mapButton.frame = mapButtonFrame;
Still, that approach is bad, please take a look at screenshots attached:
Here, as you can see, always different gap between 2 elements. How could i make it fixed?
By the way, auto layout is turned off.
Any advice would be appreciated, thanks1
This should work just fine:
[myTextView sizeToFit];//This will change the textView frame according to the text length
CGRect bFrame = self.mapButton.frame;
bFrame.origin.y = myTextView.frame.size.height + 20.0f;// The gap you want between the text and the button
self.mapButton.frame = bFrame;

Incorrect size of a textview frame (self.textview.frame.size.height)

I want to change the height of a textview depending on its content. I created the method to resize its view. The first time I call this view controller it resize properly (height = 253) but not the other times (height = 296).
I tried resizing it from viewDidAppear, viewWillAppear and viewDidLoad. The first time viewDidAppear and viewDidLoad are called. Second and following times all methods (viewDidAppear, viewWillAppear and viewDidLoad) are called. I don't know the reason and I don't know why this weird behavior, any clue?
-(void) setHeight
{
NSLog(#"Set height");
CGRect frame = descriptionTextView.frame;
frame.size.height = descriptionTextView.contentSize.height;
descriptionTextView.frame = frame;
NSLog(#"height: %f", descriptionTextView.frame.size.height);
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
totalHeight = 380+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
else{
totalHeight = 250+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
}
I use autolayout in my project but not for this view since I dont know how to properly resize a textview inside a scrollview (which also includes 2 more views with labels, images and buttons) based on the textview content with autolayout. Is it better to use autolayout than this function? Perhaps you can help me with the constraints...
You are changing only the scroll size according to your calculation but you are missing to change the size of text view itself. After setting scroll view content size update the size of the text view and all should be fine (contentSize and frame). This may look like:
[descriptionTextView setContentSize:CGSizeMake(CGRectGetWidth(descriptionTextView.frame),totalHeight)];
CGRect frame = descriptionTextView.frame;
frame.size.height = totalHeight;
[descriptionTextView setFrame:frame];
By the way: in the if-statement you can only calculate totalHeight and set it afterwards.
Additionally you do not need this line:
frame.size.height = descriptionTextView.contentSize.height;
Try this:
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = yourTextView.frame.size.width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"yourFontName" size:yourFontSize], NSFontAttributeName,
nil];
CGRect frame = [yourTextView.text boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Use this frame for your textview

Why can't I change the frame of my UILabel?

I have been trying for two days to modify the frame of a UILabel, which
is ridiculous... The UILabel is an IBOutlet, but that is not the reason
why it's not working: I tried to create a UILabel programatically and
it still didn't work. Here is how I do it:
self.descriptionLabel.text = description;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
constrainedToSize:CGSizeMake(self.descriptionLabel.frame.size.width, FLT_MAX)
lineBreakMode:self.descriptionLabel.lineBreakMode];
CGFloat frameHeight = textSize.height;
CGRect frame = self.descriptionLabel.frame;
frame.size.height = frameHeight;
self.descriptionLabel.frame = frame;
CGRect bounds = self.descriptionLabel.frame;
bounds.origin.x = self.descriptionLabel.frame.origin.x + 10.0;
bounds.size.width = self.descriptionLabel.frame.size.width - 20.0;
self.descriptionLabel.bounds = bounds;
self.descriptionLabel.numberOfLines = 0;
I already asked on IRC and they told me that there is no reason why the
frame would not be changed...
I also tried to create a frame with CGRectMake and give it arbitrary
value but that didn't do the trick either..
Does anyone know what might be the problem please?
Edit 1: I logged the frame before and after, and I got odd results:
Before: 0.000000 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000 33525.000000
The height after I set the frame is the only value that makes sense (I have a lot of text in the label on purpose).
Edit 2: I changed the code as follows, the log says that the frame
changed but on the simulator it didn't change; I proved it by adding red
borders to the layer of the UILabel.
self.descriptionLabel.text = description;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
constrainedToSize:CGSizeMake(self.view.frame.size.width, FLT_MAX)
lineBreakMode:self.descriptionLabel.lineBreakMode];
CGFloat frameHeight = textSize.height;
CGRect frame = CGRectMake(0.0, 300.0, self.view.frame.size.width, frameHeight);
self.descriptionLabel.frame = frame;
DDLogInfo(#"After: %f %f %f %f", self.descriptionLabel.frame.origin.x, self.descriptionLabel.frame.origin.y, self.descriptionLabel.frame.size.width, self.descriptionLabel.frame.size.height);
// After: 0.000000 300.000000 320.000000 585.000000
self.descriptionLabel.numberOfLines = 0;
self.descriptionLabel.layer.borderColor = [UIColor redColor].CGColor;
self.descriptionLabel.layer.borderWidth = 2;
If you're using auto layout, you shouldn't be doing any setting of frames -- either turn off auto layout, or use constraints to change the size of your label. The easiest way to do this is to give your label a height constraint (and width too, if you want a constant width) in IB, and make an IBOutlet to it. Then, in code, change the constant value of that constraint based on the value you get from sizeWithFont:constrainedToSize:lineBreakMode:.
Your label has a zero frame to begin with, so, when you do this:
CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
constrainedToSize:CGSizeMake(self.descriptionLabel.frame.size.width, FLT_MAX)
lineBreakMode:self.descriptionLabel.lineBreakMode];
You're asking the sizeWithFont method to do something effectively impossible - tell you the height that is required when no width is allowed. The response it provides (33525.000000) is probably the height if one character is allowed on each line or something like that.
So, either set the width of the label before you start, or provide the allowed width to the sizeWithFont method and then set the label size using the response (not just the height).
Also, consider what you're trying to do by changing the bounds. Comment that code out till you get the frame right at least, then you can add it back in and check it actually does what you want.

Resources