Get the Nsstring Height

How to get the size of a NSString

This is a different approach. Find out the minimum size of the text so that it won't wrap to more than one line. If it wraps to over one line, you can find out using the height.

You can use this code:

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];

300 is the width of the screen with a little space for margins. You should substitute your own values for font and size, and for the lineBreakMode if you're not using IB.

Now myStringSize will contain a height which you can check against the height of something you know is only 1 line high (using the same font and size). If it's bigger, you'll need to cut the text. Note that you should add a ... to the string before you check it again (adding the ... might push it over the limit again).

Put this code in a loop to cut the text, then check again for the correct height.

Get the NSString height

The reason what you're doing doesn't work as you would expect is because

– sizeWithFont:forWidth:lineBreakMode: 

is for "Computing Metrics for a Single Line of Text" whereas

-sizeWithFont:constrainedToSize:lineBreakMode:

is for "Computing Metrics for Multiple Lines of Text". From the documentation:

Computing Metrics for a Single Line of Text

– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

Computing Metrics for Multiple Lines of Text

– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:

Try using -sizeWithFont:constrainedToSize:lineBreakMode: instead, e.g. this is what I usually do:

CGSize maximumLabelSize = CGSizeMake(353,9999);

CGSize expectedLabelSize = [string sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];

CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;

Get the NSString height in iOS 7

Well here is a solution I use for calculating the height for iOS 6 and iOS 7, and I have passed few arguments to make it reusable.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

/**
* This method is used to calculate height of text given which fits in specific width having font provided
*
* @param text Text to calculate height of
* @param widthValue Width of container
* @param font Font size of text
*
* @return Height required to fit given text in container
*/

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
{
CGFloat result = font.pointSize + 4;
if (text)
{
CGSize textSize = { widthValue, CGFLOAT_MAX }; //Width and height of text area
CGSize size;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
//iOS 7
CGRect frame = [text boundingRectWithSize:textSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:font }
context:nil];
size = CGSizeMake(frame.size.width, frame.size.height+1);
}
else
{
//iOS 6.0
size = [text sizeWithFont:font constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
}
result = MAX(size.height, result); //At least one row
}
return result;
}

Hope this helps and yes any suggestions are appreciated. Happy Coding :)

For iOS 7 and above use below method.

+ (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
//iOS 7
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;
}

How do I calculate the size of an NSString?

To "measure" text height you could use something like this:

-(CGFloat)measureText:(NSString*)text {  
CGSize requiredSize = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(220, CGFLOAT_MAX)];
return requiredSize.height;
}

The "220" is the maximum width of the text view - change it for your case - and of course you could change the font type/size.

Also, have a look at NSString UIKit Additions Reference for more information.

How to calculate the height of the text rectangle from an NSString?

Sounds like after you get the actual font size from that function call, you need to call again with that new size:

NSString* yourString = @"SomeString";
float actualSize;
[yourString sizeWithFont:yourFont
minFontSize:minSize
actualFontSize:&actualSize
forWidth:rectWidth
lineBreakMode:breakMode];

CGSize size = [yourString sizeWithFont:[UIFont fontWithName:fontName size:actualSize]];

How to calculate the Width and Height of NSString on UILabel

That's because you did not reuse the table cell, the structure should be like:

NSString *text = [messageInfo objectForKey:@"compiled"];
if(cell == nil)
{
writerNameLabel.numberOfLines = 0;
writerNameLabel.textAlignment = UITextAlignmentRight;
writerNameLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];

I've been gone through and answered some of your question, that's correct way to write your tableview controller. And your problem will be solved.

How to get a string of a particular height in objective c

Use this code for set particular content height:

 NSString *finalString=@"d jdsfhksd fsdfhjkds fkdhsfjkdsf kdsfhjkdsf sdjkfsdfh dsfhsfisudi udsifhisdhf sdhfkh sdkf dshf ksdh fkhsd khsdfh sdkhf sdhkfhsdjkfhdshf sdfhdshf hsdfh ksdhf kdhsf hsdkfhsdkhf sdf hsdkfh dsf sdkhf ksdhf hsdfh sdhfksdhf sd ksdhsdh ksdh dhs hsdh ksdh dhs fhsdfh dsfsdfdf sdf sd sd dsf sd fsdf ds"; 
int length =200;
UIFont *font=[UIFont fontWithName:@"Helvetica" size:17.0f];
NSString *firstPartString=[NSString stringWithString:finalString];

CGSize firstPartStringSize = [firstPartString sizeWithFont:font constrainedToSize:CGSizeMake(195,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

while(firstPartStringSize.height > length)
{
firstPartString = [firstPartString substringWithRange:NSMakeRange(0, firstPartString.length-1)];
firstPartStringSize = [firstPartString sizeWithFont:font constrainedToSize:CGSizeMake(195,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
}

if(firstPartStringSize.height <= length)
{
while(![[firstPartString substringToIndex:[firstPartString length]-1] isEqualToString:@" "])
{
NSLog(@"%@",[firstPartString substringFromIndex:[firstPartString length]-1]);
if([firstPartString substringFromIndex:[firstPartString length]-1].length !=0 && ![[firstPartString substringFromIndex:[firstPartString length]-1] isEqualToString:@" "])
{
firstPartString = [firstPartString substringWithRange:NSMakeRange(0, firstPartString.length-1)];
}
else
{
break;
}

}
}

NSString *resultStr=firstPartString;

int secondPartStringLength=[finalString length]-firstPartString.length;
NSRange secondPartStringRange=NSMakeRange(firstPartString.length, secondPartStringLength);
NSString *secondPartString=[finalString substringWithRange:secondPartStringRange];


Related Topics



Leave a reply



Submit