UIScrollView size to match UITextView size - ios

I have a UIScrollView with a block of text in a UITextView. The text could be any amount of words coming from a server.
I want the scroll view to be the correct scroll size to match the size of the UITextView.
What is the current practice to do this?
Screenshot:

UITextView is an extension of UIScrollView. So you can get its content size using contentSize propery.

You can use something like below to get the height for your textview
CGSize size = [#"Your string"
sizeWithFont:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:22]
constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];
_textView.frame = CGRectMake(x,y, width, size.height);
_scrollview.frame = CGRectMake(x,y, width, size.height);
or there is a littl hack you can use :p
Use a Label to get the height for your text view.
UILabel *tempLabel = [[UILabel alloc]initWithFrmae:_textView.bounds];
tempLabel.numberOfLines = 0; // Important to do
tempLabel.text = #"Your Text";
[tempLabel sizeToFit]; // This will resize your Label and now you can use it's height to manage your textView.
Now use this tempLabel's height
tempLabel.frame.size.height

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.

UILabel.frame.size.height shows less height than actual value

I am creating a UILabel with dynamic height by calculating the length of the text with this code:
UILabel *bankFullName = [[UILabel alloc] init];
[bankFullName setLineBreakMode:NSLineBreakByWordWrapping];
[bankFullName setMinimumScaleFactor:1];
[bankFullName setNumberOfLines:0];
[bankFullName setFont:[UIFont systemFontOfSize:13]];
CGSize constraint1 = CGSizeMake(240.0f, 20000.0f);
CGSize size1 = [[bankDetail valueForKey:#"FullName"] sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
bankFullName.text = [bankDetail valueForKey:#"FullName"];
CGRect fullnameFrame = CGRectMake(15.0, 15.0, 240.0, size1.height);
bankFullName.frame = fullnameFrame;
Now I don't have any issue with the code and the label is perfect.
The problem is when I log bankFullName.frame.size.height, the height is not right. If the label has two lines, it gives the height of the label having one line and so on.
Any idea on how to get the accurate height of the label or if something I'm doing is wrong?
Thanks
Just log the height of label in this method as -[UIView layoutSubviews] get called when the size of the any view changes:-
-(void)layoutSubviews
{
[super layoutSubviews];
NSLog(#""... // log your label height to get the exact value.
}
Do not use sizeWithFont: to change the height of your label.
Use [bankFullName sizeToFit] instead. And don't forget to set the maximum number of lines to more than one.
You should use sizeThatFits: to get correct size for UILabel, because sizeWithFont and boundingRectWithSize:options:attributes:context don't include UILabel padding.
CGFloat height = [self.bankFullName sizeThatFits:CGSizeMake(240, MAXFLOAT)].height;
self.bankFullName.frame = CGRectMake(15, 15, 240, height);

UILabel text alignment issues in iphone?

I am getting JSON response from server and set that contents to UILabel and sometimes the text will be one line and sometimes the text will be 10 lines.So i display this UILabel inside the UIScrollView. But i got the empty space in UILabel
This is my screenshots..
Currently i am using storyboard and auto layout
So i want to avoid these empty space in UILabel. Please give me suggestions for this alignment issues.??
Code for changing UILabel height but this is not working for me
CGRect textRect = [strDetailsFinal boundingRectWithSize:CGSizeMake(296, FLT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName: labDetails.font}
context:nil];
CGSize size = textRect.size;
CGRect newFrame = labDetails.frame;
newFrame.size.height = size.height;
labDetails.frame = CGRectMake(160, 200, 300, newFrame.size.height);
May this help you
NSDictionary *attributesDic= [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"FontName" size:15], NSFontAttributeName,nil];
CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0)
options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDic context:nil];
CGSize size = frame.size;
and its possible answer Dynamically resize label in iOS 7 or you can try this https://gist.github.com/danielphillips/1005520
Use [labDetails sizeToFit]. If you have size constraints then use [sizeThatFits:CGSizeMake(WidthConstraint, HeightConstraint)] then use the result of that to set your label frame.
why don't you use UIButton instead of UILabel. UIButton has property to align the text vertically, while UILabel doesn't.
Use UIButton with userInteractionEnabled = NO. Check my this reply here: Vertically align UILabel
Remember one thumb rule - You should not set frame when using autolayout - You are going against Autolayout basic concept - Instead try to autolayout constraints programatically in your case
-(void)awakeFromNib{
[super awakeFromNib];
for(NSLayoutConstraint *cellConstraint in self.constraints){
[self removeConstraint:cellConstraint];
id firstItem = cellConstraint.firstItem == self ? self.contentView : cellConstraint.firstItem;
id seccondItem = cellConstraint.secondItem == self ? self.contentView : cellConstraint.secondItem;
NSLayoutConstraint* contentViewConstraint =
[NSLayoutConstraint constraintWithItem:firstItem
attribute:cellConstraint.firstAttribute
relatedBy:cellConstraint.relation
toItem:seccondItem
attribute:cellConstraint.secondAttribute
multiplier:cellConstraint.multiplier
constant:cellConstraint.constant];
[self.contentView addConstraint:contentViewConstraint];
}
}
You have taken a fix size UILable rather you have to use a dynamic size UILable.
Before that you need to calculate the size of text/string
#define FONT_SIZE 15.0f
#define LABEL_CONTENT_WIDTH 320.0f
#define LABEL_CONTENT_MARGIN 10.0f
NSString *text = #"YOUR TEXT HERE";
CGSize constraint = CGSizeMake(LABEL_CONTENT_WIDTH - (LABEL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
after calculating the size, apply them to your label like below
[labDetails setFrame:CGRectMake(labDetails.frame.origin.x, labDetails.frame.origin.y, labDetails.frame.size.width, MAX(height, 44.0f))];
And If you are using autolayout option then please check here: Link
Use Upper label frame's origin and size, and based on that set your detail labels frame, Use sizeToFit property of label.
Suppose 582,618 is your title label,
Like,
labDetails.frame=CGRectMake(5, title.frame.origin.x+title.frame.size.height+5, 300, 100);
[labDetails sizeToFit];

Table cell height by UITextView where content alignment is NSTextAlignmentJustified

I have a UITextView where content is aligned by NSTextAlignmentJustified. In the cellForRowAtIndexPath: add this text view to cell content view and change frame according it's content by
UITextView *commentsTextView = (UITextView *)[self getCommentField:comment];
[cell.contentView addSubview:commentsTextView];
CGRect tempFrame = commentsTextView.frame;
tempFrame.size.height = commentsTextView.contentSize.height;
commentsTextView.frame = tempFrame;
So far, so good. Now, how to detect contentSize of that view for heightForRowAtIndexPath:?
I tried
NSString *comment = [[self.allComments objectAtIndex:indexPath.row] objectForKey:#"commentText"];
CGSize constraintSize = CGSizeMake(315.0f, MAXFLOAT);
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:constraintSize
lineBreakMode:NSTextAlignmentJustified];
Also I tried lineBreakMode:NSLineBreakByWordWrapping and so. But cell height is always smaller when text is long, because there seems to be no way how to coumpute size of justified text (because of the white spaces in the middle of text). Font is same.
OK, i got it. UITextView has 8 pixels insect from each side, co my CGSizeMake(315.0f, MAXFLOAT); have to be CGSizeMake(299.0f, MAXFLOAT);

Dynamically Creating and Positioning UILabels of Different Sizes

I am creating dynamic UILabels and then adding them to the view. The labels are in vertical layout. Like this:
Label 1
Label 2
Label 3
Each label can be of different height. So in order to accomodate the height I use the following code. The Label 2 uses Label 1 position and height to adjust itself on the view.
-(void) createAboutLabel
{
self.aboutLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height, self.view.frame.size.width - 10, 40)];
self.aboutLabel.text = self.selectedVegetableCategory.about;
self.aboutLabel.font = [Utility getFont:#"Sinhala Sangam MN" size:14];
// [self.aboutLabel sizeToFit];
[self.aboutLabel setNumberOfLines:0];
[self.aboutLabel setLineBreakMode:UILineBreakModeWordWrap];
[self.aboutLabel sizeToFit];
[self.scrollView addSubview:self.aboutLabel];
}
I am trying to find a better way of achieving the dynamic label layout.
Any suggestions?
By passing width & font, height can be retrieved -
NSString *theText = myLabel.text;
CGFloat width = myLabel.frame.size.width ;
CGSize theSize = [theText sizeWithFont:myLabel.font constrainedToSize:CGSize(width,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
It will return the size from that extract height.
CGFloat myLabelHeight = theSize.height;
Hope this should help.

Resources