how to adjust UITextview height based on String dynamically in ios - ios

I have a simple textView who's data gets populated dynamically. I want to resize the height of the textview once the data is populated so that I don't see a vertical scroll nor the text gets clipped.I want to do this task programatically. I have a label which should be placed 20 px below height of textview like "interested".I am trying to making the code.But i got some problems like Alignment issues. If i can run the program the output will display like this.
this is my Program.please help me anybody.
lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 300, 20)];
lblHobbies.text=#"Hobbies";
lblHobbies.font=[UIFont systemFontOfSize:16.0];
lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblHobbies];
lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(10, 420, 300, strSize.height+30)];
lblInterests.text=#"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[scrollView addSubview:lblInterests];
NSDictionary *attributes = #{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect rect =
[tViewhobbies.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
tViewhobbies=[[UITextView alloc]init];
tViewhobbies.frame=CGRectMake(10, 330,300, rect.size.height);
[tViewhobbies setUserInteractionEnabled:NO];
tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewhobbies.delegate=self;
[scrollView addSubview:tViewhobbies];
NSDictionary *attributes1 = #{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect rect1 =
[tViewInterests.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes1
context:nil];
tViewInterests=[[UITextView alloc]init];
tViewInterests.frame=CGRectMake(10, 450, 300, rect1.size.height);
[tViewInterests setUserInteractionEnabled:NO];
tViewInterests.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
tViewInterests.delegate=self;
[scrollView addSubview:tViewInterests];

go through below code hope helps u
self.scrollView.contentSize = CGSizeMake(320, 700);
UILabel *lblHobbies = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 2, 75, 40)];
lblHobbies.text=#"Hobbies";
lblHobbies.font=[UIFont systemFontOfSize:16.0];
lblHobbies.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[self.scrollView addSubview:lblHobbies];
//add this after the hobbies textview so u need to calculate the height, so wy dont you put some helper method that returns the height for the text
//i took some long text that u want to display
NSString *str = #"BARCELONA, Spain -- Nokia is targeting emerging markets with three low-cost smartphones that use Google's Android operating system rather than the Windows Phone software from Microsoft, which is about to buy Nokia's phone business";
CGSize hobbiesTextViewSize = [self calculateHeightForString:str];
//now set the hobbiesTextView frame and also the text
UITextView *tViewhobbies = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + 40, hobbiesTextViewSize.width, hobbiesTextViewSize.height+ 5)];
tViewhobbies.font = [UIFont systemFontOfSize:17.0f];
tViewhobbies.text = str;//set the text hear
[self.scrollView addSubview:tViewhobbies];
//now add the lblInterests label
UILabel *lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + hobbiesTextViewSize.height + 2, 75, 40)];
lblInterests.text=#"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
[self.scrollView addSubview:lblInterests];
NSString *str1 = #"Nokia will ditch many of the Google services that come with Android, which Google lets phone makers customize at will. Instead, the new Nokia X line announced Monday will emphasize Microsoft services such as Bing search, Skype communications and OneDrive file storage. Its home screen sports larger, resizable tiles resembling those on Windows phone.";
//now calculate the height for Interests text
CGSize interestTextViewSize = [self calculateHeightForString:str1];
UITextView *tViewInterests = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.bounds.origin.x + 2, self.scrollView.bounds.origin.y + lblHobbies.frame.size.height + lblInterests.frame.size.height + tViewhobbies.frame.size.height + 4, interestTextViewSize.width + 2, interestTextViewSize.height+ 2)];
tViewInterests.font = [UIFont systemFontOfSize:17.0f];
tViewInterests.text = str1;
[self.scrollView addSubview:tViewInterests];
}
//our helper method
- (CGSize)calculateHeightForString:(NSString *)str
{
CGSize size = CGSizeZero;
UIFont *labelFont = [UIFont systemFontOfSize:17.0f];
NSDictionary *systemFontAttrDict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];
NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:str attributes:systemFontAttrDict];
CGRect rect = [message boundingRectWithSize:(CGSize){320, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];//you need to specify the some width, height will be calculated
size = CGSizeMake(rect.size.width, rect.size.height + 5); //padding
return size;
}

Here is how I did it:
+(CGFloat)getLabelDymanicHeightOfStringWithText:(NSString *)text andFont:(UIFont *)font andFrame:(CGRect )frame {
CGSize maxSize = CGSizeMake(frame.size.width, 999999.0);
int height = 0;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
nil];
if (IS_IOS_6)//iOS 6 macro
{
CGSize size = [text sizeWithFont:font
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
height = size.height;
}
else
{
CGRect frame = [text boundingRectWithSize:maxSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
height = frame.size.height;
}
return height+5;
}
Pass this method your text text font and frame.

You can use custom UITextView to fix the problem:
Kindly check it out: https://github.com/HansPinckaers/GrowingTextView
Textview will set height dynamically based on input string from the user.
Hope, This will help you,
:)

enter code hereYou can use label.
myLabel.text = #"Your String";
CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:CGSizeMake(320,MAX_HEIGHT_YOU_WANT_TO_SET)
lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = myLabel.frame;
frame.size.height = labelSize.height;
myLabel.frame = frame;

you can simply change the height of the textView to the height of the textView.contentSize.height. Doing this you textView will update and show all the text it contains.
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width,textView.contentSize.height);
Hope this helps you.
Happy Coding :)

Related

how to do truncation of text in UILabel

I have problem with truncation of text in UILabel even I have set lineBreakMode = NSLineBreakByWordWrapping perfectly.
Here is my code snippet :
lblSelectedText = [[UILabel alloc] init];
lblSelectedText.numberOfLines = 0;
lblSelectedText.lineBreakMode = NSLineBreakByWordWrapping;
lblSelectedText.font = [UIFont fontWithName:#"MyriadPro-Regular" size:(IS_IPAD_PRO?13.0:9.0)];
lblSelectedText.textAlignment = NSTextAlignmentCenter;
lblSelectedText.textColor = [UIColor lightGrayColor];
lblSelectedText.text = strKey; // Here text will be dynamic
CGFloat width = 150;
CGSize strSize = [self findHeightForText:strKey havingWidth:width andFont:lblSelectedText.font];
lblSelectedText.frame = CGRectMake(12, 10, CGRectGetWidth(aContainerController.view.frame)-20, strSize.height+15);
- (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
{
CGSize size = CGSizeZero;
if (text)
{
if (IS_ENGLISH) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height + 10);
}
else {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height + 1);
}
}
return size;
}
Please note that these labels are in collection view cell. Here are some reference images.
If anyone have solution of this problem please share with me...
Thanks.
As you have mentioned in comment that some characters goes in to next line that means there is not enough width to fill every letter in single line thats why it is going to second line as you set number of line to 0(i.e. multiple lines). So there is nothing is wrong in it. It is normal behavior according to your setup made in code.
now if you want that it not goes to second line then set numberOfLines property to 1.
Another option is you can set minimunScaleFactor from 0 to 1 to resize your font to fit in available width.
You can do it something like,
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5f;
So, It will reduce font size to half of the actual to fit it in availabel width.
And once try by changing linebreakmode to NSLineBreakByTruncatingTail also instead of NSLineBreakByWordWrapping.
Check this:-
label.minimumScaleFactor = 0.5f;
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
[label sizeToFit];

Problems with sizes and lines in UILabel

I have a little question. I developed an application that automatically fills a data set obtained from a SQLite database. These data are drawn from dynamic way, and between the data I have a frame where I insert labels dynamically. I never know the exact number of labels inside these frame because I take data from different tables of my Database. The problem I have with the labels which texts within them do not fill within the width of the label. I tried to use label.linebreakmode but still makes the break. I post the code:
There I have many objects taken from previous code, like widthFormato and widthImageFormato
if([tipoVino length]!=0){
UILabel *lblFormato = [[UILabel alloc] init];
labelX = ((widthFormato-widthImageFormatos) / 2)+10;
CGRect labelFrame = lblFormato.frame;
labelFrame.size.width = widthImageFormatos;
labelFrame.origin.x = labelX;
labelFrame.origin.y = labelY+25;
lblFormato.frame = labelFrame;
lblFormato.numberOfLines = 0;
lblFormato.lineBreakMode = NSLineBreakByWordWrapping;
[lblFormato setText:[NSString stringWithFormat:#"- %#",tipoVino]];
lblFormato.textColor = [UIColor whiteColor];
labelY = lblFormato.frame.origin.y;
[formatosImageView addSubview:lblFormato];
}
I think that you want to create labels with flexible height (not all have the same size), and must fix the width of formatosImageView.
EDITED FOR iOS7
I was using sizeWithFont that is deprecated in iOS7, I changed it for boundingRectWithSize
Try this:
UILabel *lblFormato = [[UILabel alloc] init];
lblFormato.numberOfLines = 0;
lblFormato.lineBreakMode = NSLineBreakByWordWrapping;
[lblFormato sizeToFit];
NSString *string = [NSString stringWithFormat:#"- %#", tipoVino];
[lblFormato setText: string];
//Calculate the size of the container of the lblFormato
CGSize size = [string boundingRectWithSize:CGSizeMake(widthImageFormatos, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: [UIFont fontWithName:#"customFont" size:fontSize]} context:nil];
lblFormato.frame = CGRectMake(labelX, labelY + 25, size.width, size.height);
and you must update the labelY with:
labelY = lblFormato.frame.size.height;
Maybe it helps you.
With regards #spinillos answer, for iOS 7:
NSDictionary *attributes = #{ NSFontAttributeName : [UIFont systemFontOfSize:<#fontSize#>] };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString: <#string#>,
attributes:attributes];
CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(<#width#>, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];

UILabel Dynamic Width calculation Multiple Lines

I have UILabel where i have 2 lines of text. I used the below code to calculate the dynamic width and height
CGSize expectedLabelSize = [[aLabel text] sizeWithFont:aLabel.font
constrainedToSize:aLabel.frame.size
lineBreakMode:aLabel.lineBreakMode];
CGRect rect = [aLabel frame];
rect.size.width = expectedLabelSize.width;
[aLabel setFrame:rect];
Now if there is multiple lines in the UILabel how can i detect the width. See the image for more clearence.
set property for Label
label.numberOfLines = 0;
CGFloat labelHeight= [self heightOfLabelWithIngredientLine:pixers.name];
[label setFrame:CGRectMake(10,10,100,labelHeight)];
- (CGFloat)heightOfLabelWithIngredientLine:(NSString *)stringLabel
{
CGRect frame;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:#"Helvetica" size:14.0f], NSFontAttributeName, nil];
frame = [stringLabel boundingRectWithSize:CGSizeMake(20.0f, 999.0f) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];
return frame.size.height;
}
Customize your label properties in
- (CGFloat)heightOfLabelWithIngredientLine:(NSString *)stringLabel
method.

Resize UILabel based on content

I have a UILabel, his text size has the property
title.adjustsFontSizeToFitWidth = YES;
that prevents me from using standard methods to resize the UILabel. I read on another post here that I'm supposed to use the function
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode
from this answer: How to figure out the font size of a UILabel when -adjustsFontSizeToFitWidth is set to YES?
Now, i can't figure out how to make it work.. this is the actual code
UIFont *font = [UIFont fontWithName:#"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = #"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;
CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x,
title.frame.origin.y,
size.width,
size.height);
The UILabel get resized wrongly, as if the font size it's still 200..
Any clues? Thanks!
I have some code you could use on my github, check it out, it's a category for UILabel, you need to set the frame width and when you can resizeToFit on the UILabel, it adjusts the height to fit the content, and returns the y position of the end of the label so you can adjust any content appearing after it.
https://gist.github.com/1005520
I'd suggest filing this as a bug.
The size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: has the correct width, but not the height does not account for the actual font size.
It seems likely that UILabel also has this bug. Changing the size of a label to match the height of the text in a font of the size returned by -sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: will incorrectly vertically position the text within the label.
A work-around is to calculate the correct height, and change the font on the label to with the actual font size:
CGFloat pointSize = 0.0f;
CGRect frame = title.frame;
frame.size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
UIFont *actualFont = [UIFont fontWithName:#"Marker Felt" size:pointSize];
CGSize sizeWithCorrectHeight = [title.text sizeWithFont:actualFont];
frame.size.height = sizeWithCorrectHeight.height;
title.frame = frame;
title.font = actualFont;
Try to create font with smaller fontSize. For example:
UIFont *font = [UIFont fontWithName:#"Marker Felt" size:20];
but to actualFontSize pass link to CGFloat == 200.
UPDATED:
try this then:
UIFont *font = [UIFont fontWithName:#"Marker Felt" size:20];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = #"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;
CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font minFontSize:title.minimumFontSize actualFontSize:&pointSize forWidth:width lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, size.width, size.height);
font = [UIFont fontWithName:#"Marker Felt" size:200];
title.font = font;
UILabel * label = [[UILabel alloc] init];
[label setNumberOfLines:0];
label.text=[detailDict valueForKey:#"method"];
[label setFont:[UIFont fontWithName:#"Georgia" size:16.0]];
CGSize labelsize=[label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
int y=0;
label.frame=CGRectMake(38, y, 245, labelsize.height);
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
scrollView.showsVerticalScrollIndicator = NO;
y+=labelsize.height;
[scrollView setContentSize:CGSizeMake(200,y+50)];
[scrollView addSubview:label];
[label release];
I think you should use this code and i am using the label on scrollview for a big text you can also do so
Have you tried intrinsicContentSize?
myLable.numberOfLines = 0
myLable.frame = CGRect(x: 10, y: 10, width: 300, height: lblSuperscript.intrinsicContentSize().height)

How to calculate UILabel width based on text length?

I want to display an image next to a UILabel, however UILabel has variable text length, so I don't know where to place the image. How can I accomplish this?
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?
this question might have your answer, it worked for me.
For 2014, I edited in this new version, based on the ultra-handy comment by Norbert below! This does everything.
// yourLabel is your UILabel.
float widthIs =
[self.yourLabel.text
boundingRectWithSize:self.yourLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{ NSFontAttributeName:self.yourLabel.font }
context:nil]
.size.width;
NSLog(#"the width of yourLabel is %f", widthIs);
yourLabel.intrinsicContentSize.width for Objective-C / Swift
In swift
yourLabel.intrinsicContentSize().width
The selected answer is correct for iOS 6 and below.
In iOS 7, sizeWithFont:constrainedToSize:lineBreakMode: has been deprecated. It is now recommended you use boundingRectWithSize:options:attributes:context:.
CGRect expectedLabelSize = [yourString boundingRectWithSize:sizeOfRect
options:<NSStringDrawingOptions>
attributes:#{
NSFontAttributeName: yourString.font
AnyOtherAttributes: valuesForAttributes
}
context:(NSStringDrawingContext *)];
Note that the return value is a CGRect not a CGSize. Hopefully that'll be of some assistance to people using it in iOS 7.
Swift 4 Answer who are using Constraint
label.text = "Hello World"
var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(attributes: [NSFontAttributeName: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet
Swift 5 Answer who are using Constraint
label.text = "Hello World"
var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(withAttributes: [NSAttributedString.Key.font: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet
In iOS8 sizeWithFont has been deprecated, please refer to
CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:#{NSFontAttributeName : [UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];
You can add all the attributes you want in sizeWithAttributes.
Other attributes you can set:
- NSForegroundColorAttributeName
- NSParagraphStyleAttributeName
- NSBackgroundColorAttributeName
- NSShadowAttributeName
and so on. But probably you won't need the others
CGRect rect = label.frame;
rect.size = [label.text sizeWithAttributes:#{NSFontAttributeName : [UIFont fontWithName:label.font.fontName size:label.font.pointSize]}];
label.frame = rect;
Here's something I came up with after applying a few principles other SO posts, including Aaron's link:
AnnotationPin *myAnnotation = (AnnotationPin *)annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
self.backgroundColor = [UIColor greenColor];
self.frame = CGRectMake(0,0,30,30);
imageView = [[UIImageView alloc] initWithImage:myAnnotation.THEIMAGE];
imageView.frame = CGRectMake(3,3,20,20);
imageView.layer.masksToBounds = NO;
[self addSubview:imageView];
[imageView release];
CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]];
CGRect newFrame = self.frame;
newFrame.size.height = titleSize.height + 12;
newFrame.size.width = titleSize.width + 32;
self.frame = newFrame;
self.layer.borderColor = [UIColor colorWithRed:0 green:.3 blue:0 alpha:1.0f].CGColor;
self.layer.borderWidth = 3.0;
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(26,5,newFrame.size.width-32,newFrame.size.height-12)];
infoLabel.text = myAnnotation.title;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.textColor = [UIColor blackColor];
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.font = [UIFont systemFontOfSize:12];
[self addSubview:infoLabel];
[infoLabel release];
In this example, I'm adding a custom pin to a MKAnnotation class that resizes a UILabel according to the text size. It also adds an image on the left side of the view, so you see some of the code managing the proper spacing to handle the image and padding.
The key is to use CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]]; and then redefine the view's dimensions. You can apply this logic to any view.
Although Aaron's answer works for some, it didn't work for me. This is a far more detailed explanation that you should try immediately before going anywhere else if you want a more dynamic view with an image and resizable UILabel. I already did all the work for you!!

Resources