This question has asked many times, however I have not clear, got wrong output. So please anyone help..
CGSize maximumSize = CGSizeMake(208, 21);
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [my_string sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.my_label.lineBreakMode];
my_label.numberOfLines = 0;
my_label.frame.size = myStringSize;
I have a label of size (208, 21), I have used the following code to get actual height required for NSString with respect to my label width, I want fixed width, only height need to vary so I can set in label. But it always give lower height than actual.. Am I doing anything wrong here..
thanks..
In your example use a maximum size with a (very) large height instead of restricting the height:
CGSize maximumSize = CGSizeMake(208, CGFLOAT_MAX);
This way there will always be enough height to expand to, while limiting the width to the width you actually want.
Try this :
-(CGSize) calculateWidthOfString:(NSString*)textString withFont:(UIFont*)font
{
CGSize maximumSize = CGSizeMake(9999, 22);
CGSize size = [textString sizeWithFont:font
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
call this function with stringObject and Font Name, Size
it returns width of string with constant height "22" change this value as u want.
hope this will helps u.
+ (CGSize) calculateLabelHeightWith:(CGFloat)width text:(NSString*)textString
{
CGSize maximumSize = CGSizeMake(width, 9999);
CGSize size = [textString sizeWithFont:[UIFont fontWithName:#"Helvetica" size:24]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
+ (CGSize) calculateLabelWidthOfString:(NSString*)textString withFont:(UIFont*)font
{
CGSize maximumSize = CGSizeMake(9999, 22);
CGSize size = [textString sizeWithFont:font
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
use these two for height or width
Related
Currently my label is having width of 100 pixels by default.
the text on label varies dynamically
my requirement is that
The label size should change as per text and its width should limited to 300 pixels.
After 300 pixels the text goes to next line and the height of the label has to be increses.
how to achieve this scenario..
self.messageLabel = [[UILabel alloc] init];
self.messageLabel.frame = CGRectMake(0, 0, 100, 20);
self.messageLabel.textAlignment = NSTextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:16];
messageLabel.numberOfLines = 0;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.view addSubview:messageLabel];
to get the text size :
CGSize labelSize = [self.messageLabel.text sizeWithFont:self.messageLabel.font];
And i can able to Change the width of label as per text
self.messageLabel.frame = CGRectMake(0, 0, self.frame.size.width-10, 20);
But how to move the text to next line after every 300 pixels and
label size increase in height as well.
-(CGSize)getLabelSize:(NSString *)text
{
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize;
}
Use this method.
I hope it helps you.
How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to that text length. Is it possible?
Actually I have Two UIlabels. I need to place these two nearby. But if the first label's text is too small there will be a big gap. I want to remove this gap.
//use this for custom font
CGFloat width = [label.text sizeWithFont:[UIFont fontWithName:#"ChaparralPro-Bold" size:40 ]].width;
//use this for system font
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;
label.frame = CGRectMake(point.x, point.y, width,height);
//point.x, point.y -> origin for label;
//height -> your label height;
Function sizeWithFont: is deprecated in iOS 7.0, so you have to use sizeWithAttributes: for iOS 7.0+. Also to suport older versions, this code below can be used:
CGFloat width;
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0)
{
width = [text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:16.0 ]].width;
}
else
{
width = ceil([text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:16.0]}].width);
}
Using function ceil() on result of sizeWithAttributes: is recommended by Apple documentation:
"This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function."
sizeWithAttributes
// In swift 2.0
let lblDescription = UILabel(frame: CGRectMake(0, 0, 200, 20))
lblDescription.numberOfLines = 0
lblDescription.text = "Sample text to show its whatever may be"
lblDescription.sizeToFit()
// Its automatically Adjust the height
Try these options,
UIFont *myFont = [UIFont boldSystemFontOfSize:15.0];
// Get the width of a string ...
CGSize size = [#"Some string here" sizeWithFont:myFont];
// Get the width of a string when wrapping within a particular width
NSString *mystring = #"some strings some string some strings...";
CGSize size = [mystring sizeWithFont:myFont
forWidth:150.0
lineBreakMode:UILineBreakModeWordWrap];
You can also try with [label sizeToFit]; Using this method, you can set frame of two labels as,
[firstLabel sizeToFit];
[secondLabel sizeToFit];
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height);
sizeWithFont constrainedToSize:lineBreakMode: is the original method to use. Here is an example of how to use it is below:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
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;
just use to if you using constrain in your view or xib or cell
[LBl sizeToFit];
if its not working then
dispatch_async(dispatch_get_main_queue(), ^{
[LBl sizeToFit];
});
Try the following:
/* Consider these two labels as the labels that you use,
and that these labels have been initialized */
UILabel* firstLabel;
UILabel* secondLabel;
CGSize labelSize = [firstLabel.text sizeWithFont:[UIFont systemFontOfSize:12]];
//change the font size, or font as per your requirements
CGRect firstLabelRect = firstLabel.frame;
firstLabelRect.size.width = labelSize.width;
//You will get the width as per the text in label
firstLabel.frame = firstLabelRect;
/* Now, let's change the frame for the second label */
CGRect secondLabelRect;
CGFloat x = firstLabelRect.origin.x;
CGFloat y = firstLabelRect.origin.y;
x = x + labelSize.width + 20; //There are some changes here.
secondLabelRect = secondLabel.frame;
secondLabelRect.origin.x = x;
secondLabelRect.origin.y = y;
secondLabel.frame = secondLabelRect;
I want to change the uilabel height as per content and display it in a uitableview cell, there is a custom cell and cell is expand as per uilabel height
When button is pressed then and then cell height is expand as per the uilabel height
Thank you in Advance :-)
//Calculate the size based on the font and linebreak mode of your label
CGSize maxLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [myString sizeWithFont:myLabel.font
constrainedToSize:maxLabelSize
lineBreakMode:myLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = myLabel.frame;
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;
Try this
[label sizeToFit];
Note that label will increase it's height keeping it's width the same.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = displayText;
//CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize constraint = CGSizeMake(tableView.frame.size.width - 30, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + 30;
}
Calculate the size of the label text & than set coordinates as
CGSize labelWidth=[_label.text sizeWithFont:[UIFont fontWithName:#"Verdana" size:12] constrainedToSize:CGSizeMake(200, 15) lineBreakMode:UILineBreakModeTailTruncation];
Unless I am reading the docs wrong, it seems like this method sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: is not returning the proper CGSize. Here's a sample code
NSString *someText = #"$";
CGFloat actualFontSize;
CGSize textSize = [someText sizeWithFont:[UIFont fontWithName:#"Futura-CondensedMedium" size:100.0] minFontSize:1.0 actualFontSize:&actualFontSize forWidth:10.0 lineBreakMode:UILineBreakModeClip];
The height of textSize returned is what it would be if the font size is 100.0, not what it'd be at the new reduced font size.
If you then take actualFontSize and do sizeWithFont:forWidth:lineBreakMode::
CGSize newTextSize = [someText sizeWithFont:[UIFont fontWithName:#"Futura-CondensedMedium" size:actualFontSize] forWidth:10.0 lineBreakMode:UILineBreakModeClip];
Now newTextSize will contain the correct height.
Anyone else encountered this?
The problem is your width. Because you have set the minimum font to 1.0 pixels and the width only to 10.0 your textSize will automatically adjust to a lower textSize fitting the 10.0 width that you set yourself.
CGSize textSize = [someText
sizeWithFont:[UIFont fontWithName:#"Futura-CondensedMedium" size:100.0]
minFontSize:1.0 //the troublemaker
actualFontSize:&actualFontSize
forWidth:10.0 //the troublemaker
lineBreakMode:UILineBreakModeClip];
The other one you posted will return the proper size because you didn't set the minimum textFont and you put your width just right.
CGSize newTextSize = [someText
sizeWithFont:[UIFont fontWithName:#"Futura-CondensedMedium"
size:actualFontSize]
forWidth:currencyRect.size.width //probably not 10 pixels wide
lineBreakMode:UILineBreakModeClip];
I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2.
This is how I do it for UITableViewCellStyleDefault:
CGSize textSize = {300.f, 200000.0f};
CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);
And for UITableViewCellStyleValue2:
CGSize textSize = {207.f, 200000.0f};
CGSize size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);
My issue it that they return incorrect heights and I think it's the textSize where I use incorrect numbers. With long texts, the bottom part gets cut short (usually just a line with a few words), and for both CellStyles they have weird spacing above and below the text in the cell.
For UITableViewCellStyleValue2 I got the width size (207.0f) from making the text.backgroundColor red and then just calculating the size of the box. 300.0f width for UITableViewCellStyleDefault is how big the cell is with UITableViewStyleGrouped.
Does anyone know which values I need to use to properly calculate the size of an NSString and thereby get appropriate height for my cells?
Thanks
Here is the code I am using for this. It works like a charm for one type of cell. It may have some useful parts for your application.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:#"Text"]);
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 15;}
When you create your cell, are you using the same font as the one you use to measure?
I used the tutorial on this page and everything worked for me. It might be useful to you too:
It seeems you've figured out that you were using the wrong text size. You might want to just use the font and lineBreakMode properties of the label to avoid this problem in the future, especially if you change them in the cell at a later time. Also, for readability's sake, I would avoid adding to the height before returning a number. Instead I'd try something like this:
CGSize textSize = CGSizeMake( 300.0, 1979 );
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ]
constrainedToSize:textSize
lineBreakMode:[[ cell textLabel ] lineBreakMode ]];
result = MAX( size.height + 30, 44.0f );
I used 1979 because according to the documentation, one should not return values larger than 2009.
If you want to calculate the height properly for cellvalue2 you should use:
[UIFont boldSystemFontOfSize:15.0f] and as linebreakmode: NSLineBreakByTruncatingTail
If you don't use bold, the text will fill correctly but you will lose the correct vertical padding.
The rest of your calculation are correct.
When is working for me is to compute the constraintSize based on the width of the table view:
CGSize constraintSize = CGSizeMake(tableView.frame.size.width * 0.6, 2009);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
Easiest way to do this:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 10.0f;
if(indexPath.section == 1) // here i compare the sections
{
result = 400.0f;
}
return result;
}
Return the values based on the sections you have. This will give you custom row heights.