Naresh
Naresh

Reputation: 363

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

Hi in my application I have a lengthy string. From that string I have to substring a part of a string where the string height to be 500. So please let me know how to get the a part of a string.

For your reference I am sharing the code what I used for getting string height

 CGSize finalStringSize = [finalString sizeWithFont:font constrainedToSize:CGSizeMake(195,length) lineBreakMode:NSLineBreakByWordWrapping];

Here finalstringSize.height is 500. Now any one help me to get a substring from final string where that substring height to be 500.

Please help me.

enter image description here

Upvotes: 1

Views: 678

Answers (2)

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

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

Upvotes: 2

Goran
Goran

Reputation: 675

As per my comment, you can try this:

CGSize finalStringSize = [finalString sizeWithFont:font constrainedToSize:CGSizeMake(195,length) lineBreakMode:NSLineBreakByWordWrapping];
while( finalStringSize.height > 500 ){
    finalString = [finalString substringWithRange:NSMakeRange(0, finalString.length-1)];
    finalStringSize = [finalString sizeWithFont:font constrainedToSize:CGSizeMake(195,length) lineBreakMode:NSLineBreakByWordWrapping];
}
NSLog(finalString); // and that is your end string...

As per your comment below, you can use CTFramesetterSuggestFrameSizeWithConstraints method to find the NSRange that you can use in [finalString substringWithRange:range]; to get your final result.

Documentation for that method is here.

Upvotes: 0

Related Questions