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.
Related
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.
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;
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;
I have a UISlider that changes the font size of a UITextView. Before changing the font size, I would like to check if the height of my UITextView will be inferior to the height of its view container. The problem is that sizeToFit and sizeWithAttribute are returning different heights for the same font size.
To do that i'm trying to get the height of my UITextView with a custom fontSize, but i cannot figure out a way to do so, and don't understand why I don't get the same height when I do:
self.photoDescription.text = #"Test"
self.photoDescription.font = [self.photoDescription.font fontWithSize:18];
[self.photoDescription sizeToFit];
NSLog(#"real height %f", self.photoDescription.frame.size.height);
//getting : 37.5
and:
NSLog(#"calculated height %f", [self.photoDescription.text sizeWithAttributes:#{NSFontAttributeName:[self.photoDescription.font fontWithSize:18]}].height);
//getting: 20.97
same thing for:
CGRect textRect = [self.photoDescription.text boundingRectWithSize:CGSizeMake(320, 300)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[self.photoDescription.font fontWithSize:18]}
context:nil];
CGSize size = textRect.size;
NSLog(#"height %f", size.height);
// getting : 20.97
Any thoughts on this?
I finally found an answer to my question with Jordan Montel Solution:
iOS7 UITextView contentsize.height alternative
I used his function to get the width :
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
I have problem with calculating rect from given string.
Here are my strings:
_name = #"Any veryveryveryvery key here:";
_value = #"Any very very very very long value";
and my layoutSubviews method:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = self.contentView.bounds;
CGFloat verticalMargin = 5.0f;
//calculate rect for given name string:
CGFloat halfScreen = CGRectGetMidX(rect); //160points in portrait
//_nameLabel:
CGRect nameRect =
[_name boundingRectWithSize:CGSizeMake(halfScreen, CGRectGetHeight(rect))
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:#{NSFontAttributeName: [UIFont fontWithName:kHelveticaFont size:_nameLabel.font.pointSize]}
context:nil];
//name rect = origin=(x=0, y=0) size=(width=147.53174, height=34.5) - WHY width = 147.53174 NOT halfScreen = 160points?
[_nameLabel setFrame:CGRectMake(0.0f,
verticalMargin,
ceilf(CGRectGetWidth(nameRect)),
CGRectGetHeight(rect) - verticalMargin * 2)];
//calculate rect for given value string:
//_valueLabel:
CGFloat maxWidth = CGRectGetWidth(rect) - CGRectGetMaxX(_nameLabel.frame); //172points in portrait
CGRect valueRect =
[_value boundingRectWithSize:CGSizeMake(maxWidth, CGRectGetHeight(rect))
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:#{NSFontAttributeName: [UIFont fontWithName:kHelveticaFont size: _valueLabel.font.pointSize]}
context:nil];
//valueRect = origin=(x=0, y=0) size=(width=160.03418, height=34.5) - WHY width = 160.03418 NOT maxWidth = 172 points?
[_valueLabel setFrame:CGRectMake(CGRectGetMaxX(_nameLabel.frame),
verticalMargin,
ceilf(CGRectGetWidth(valueRect)),
CGRectGetHeight(rect) - verticalMargin * 2)];
}
And result:
Yellow background is half Width of Screen (160 points). Green background is another 160 points. I want to have _nameLabel with max size equal to half screen width.
Anyone has ANY idea how to calculate it properly? Question are also commented in code. Thank you in advance for help.
NOTE: I'm developing an app only for iOS7.
EDIT:
Note, that if _name will be much shorter, for example "key", i want to have _valueLabel next to _nameLabel like this (and it works as it should):
I solved my problem. Actually I dived deep in apple documentation and the problem was generated by this argument:
NSStringDrawingUsesLineFragmentOrigin // which means: The specified origin is the line fragment origin, not the base line origin
in line:
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
Thats why method:
(CGRect)boundingRectWithSize:options:attributes:context:
gave me wrong rect dimensions. Now everything works as should.
_valueLabel.text = _value;
CGSize fitSize = [_valueLabel sizeThatFits:CGSizeMake(FLT_MAX, 18.0)];
CGRect newFrame = _valueLabel.frame;
newFrame.size.width = if fitSize.width > 160.0 ? 160.0 : fitSize.width
_valueLabel.frame = newFrame;
Obviously your code has a few more calculations to move the second UITextField over but hence use the same fitSize above to calculate the origin of the second UITextField plus your margin. Hope that helps get you your issue solved.