How to handle Storyboard ViewControllers in iOS? - ios

I have added 3 Views in ViewController, if 2nd/middle view has no text or it has one line text, the height of the middle view should be 30 and last view should move up at run time.

ADD THIS TO YOUR CODE:
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

ADD this CODE
[yourlabel sizeToFit ];
or
[yourView sizeToFit];

Related

Why is so much extra space being added to my UILabel when I try to change height to fit text?

I have no idea why the height of my UILabel is expanding to such a great height. It leaves the text of my UILabel in the centre. I don't want all of this extra space...
Unwanted extra space
Here is my code (I set the text before this point):
self.infoDescription.numberOfLines = 0;
[self.infoDescription sizeToFit];
self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
Please help :( I just want the height of the UILabel to fit the text exactly.
First set the frame and then make size to fit of label.
**self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
[self.infoDescription sizeToFit];**
Try this:
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = #"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
(original source)
Please check this answer it works for me and it is exactly what you are looking for.
//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

iOS auto adjust label height

I'm using the following code to auto adjust the height of a label in a UITableView. It works the majority of the time, but certain times text is cut off. Is there something wrong with my code, or anything else I need to add?
UILabel *textLabel = ((UILabel *)[cell viewWithTag:3]);
textLabel.text = text;
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [text sizeWithFont:textLabel.font constrainedToSize:maximumLabelSize lineBreakMode:textLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = textLabel.frame;
newFrame.size.height = expectedLabelSize.height;
textLabel.frame = newFrame;
In iOS 7 sizeWithFont: constrainedToSize: lineBreakMode: is deprecated, now you should use:
CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
CGRect labRect = [someText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:textLabel.font} context:nil];
textLabel.frame = CGRectMake(0, 0, maxSize.width, labRect.size.height);
textLabel.text = someText;

how to expand the UITextview when user clicks on "more " Button in ios

There are applications with a UITextview that shows a given amount of lines and when the user clicks more it expands and shows the rest,how to add more and less button.Please give me any idea. i am new to the programming.I know how to write the code of UITextview.But i dont know how to write the code of More or less button for UITextview.Thanks in advance.
1.Check UItextView size base on text and Font:
// return the size for UITextView for both IOS7 and IOS6
-(CGSize) getSizeWithMaxLabelSize:(CGSize) maxLabelSize forTextViewWithText:(NSString *) text
{
CGSize expectedLabelSize;
CGSize maximumLabelSize = maxLabelSize;
if (SYSTEM_VERSION_GREATER_THAN(#"6.2")) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] forKey: NSFontAttributeName];
CGRect rect =[text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
expectedLabelSize = rect.size;
expectedLabelSize.height = ceilf(expectedLabelSize.height);
expectedLabelSize.width = ceilf(expectedLabelSize.width);
} else {
expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
}
return expectedLabelSize;
}
2.Change UITextView frame.size.height
CGRect frame = _textView.frame;
frame.size.height = [self getSizeWithMaxLabelSize:CGSizeMake(_textView.frame.size.height, NSIntegerMax) forTextViewWithText:_textView.text].height;
_textView.frame = frame;
3.Change subviews layout
// this will work only if you textview is on the top of other views.
[UIView animateWithDuration:0.25 animations:^{
for (UIView *subview in self.view.subviews)
subview.transform = CGAffineTransformMakeTranslation(0, _textView.frame.size.height);
}];
Should be it.

UILabel won't adjust height to fit content

I can't get this dynamic label to resize its height according to the content. It logs the correct info but the label is always the same height. Any ideas?
self.descriptionLabel.text = string;
self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
self.descriptionLabel.numberOfLines=0;
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [string sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
//adjust the label the the new height.
CGRect newFrame = self.descriptionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
NSLog(#"the height %f", newFrame.size.height);
self.descriptionLabel.frame = newFrame;
CGSize constraintSize = CGSizeMake(requiredWidth, MAXFLOAT);
CGSize labelSize = [requiredLableText sizeWithFont:requiredFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
returned size will give u the dynamic height of the UILabel

UITextView with dynamically height

I have a textView which insert different texts with different lengths, some are short and others are long.. The UITextView is subviews of a scrollView. How can I dynamically set the height of UITextView according to the length of the entered text?
This code in ViewDidLoad doesn't work:
self.textView.contentSize = [self.textView.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, self.textView.contentSize.height) lineBreakMode:UIViewAutoresizingFlexibleHeight];
This does not work because your contraint is the current contentSize of the TextView. You should put the maximum size that you want.
For example your code could be like this :
#define kMaxHeight 100.f
self.textView.contentSize = [self.textView.text sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(100, kMaxHeight)
lineBreakMode:UIViewAutoresizingFlexibleHeight];
Hope this helps
As UITextView is a subclass of UIScrollView , so may be this will be helpful:
UITextView *textView = [UITextView new];
textView.text = #"Your texts ......";
CGSize contentSize = textView.contentSize ;
CGRect frame = textView.frame ;
frame.size.height = contentSize.height ;
textView.frame = frame ;
Below coding is working.Please try it
// leave the width at 300 otherwise the height wont measure correctly
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 100.0f)];
// add text to the UITextView first so we know what size to make it
textView.text = [_dictSelected objectForKey:#"body"];
// get the size of the UITextView based on what it would be with the text
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
Calculate string size that fits in UITextView:
[yourString sizeWithFont:yourtxtView.font
constrainedToSize:CGSizeMake(yourtxtView.frame.size.width,1000)
lineBreakMode:UILineBreakModeWordWrap];
Change frame of UITextView
yourtxtView.frame = CGRectMake(yourtxtView.frame.origin.x,yourtxtView.frame.origin.y,yourtxtView.frame.size.width,yourtxtView.frame.size.height+20);

Resources