Gypsa
Gypsa

Reputation: 11314

Getting text based on height

We all know that we can calculate the height of a label or any control according to the text. Like this:

    NSString *text=@"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe";     
    labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)];
    NSLog(@"labelsize.height%f",labelsize.height);

Now suppose I get height=270. Now I want only that text which lies in 200 height. Like My label height is 200 and I want that till 200 height text comes in label and rest of the text should show in another label. So I want to ask if it is possible to get the text based on height.

Thanks in advance!

Upvotes: 5

Views: 211

Answers (2)

Sonu
Sonu

Reputation: 957

As per your need you can change the label text depend on your interest. Here is my sample code.

NSMutableString *tmpLabel2=[[NSMutableString alloc]init];
NSString *text=@"Hello friend what r u doin..? what is going on in your company.. Tell me something yar i want to meet with u whenever u free just call me i will be der ok rest is perfect. talk u later…";     
NSMutableString *tmpLabel1 = [[NSMutableString alloc] initWithString:text];
NSRange range = NSMakeRange([tmpLabel1 length] - 1, 1);

CGSize  labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
while ([tmpLabel1 sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > 200) {

    unichar Char=[tmpLabel1 characterAtIndex:[tmpLabel1 length]-1];
    NSString*strTemp=[NSString stringWithFormat:@"%C",Char];
    [tmpLabel2 insertString:strTemp atIndex:0];
    [tmpLabel1 deleteCharactersInRange:range];
    range.location--;
}

label.frame=CGRectMake(50, 50, labelsize.width, 200);
label.text=tmpLabel1;
label.font=[UIFont fontWithName:@"Arial" size:14];
label.numberOfLines=0;
label.clipsToBounds=YES;
label.adjustsFontSizeToFitWidth=YES;
label.lineBreakMode=UILineBreakModeCharacterWrap;
label.backgroundColor=[UIColor grayColor];

NSLog(@"first Label is: %@", tmpLabel1);
NSLog(@"Second Label is: %@", tmpLabel2);

}

Upvotes: 1

Infinite Possibilities
Infinite Possibilities

Reputation: 7466

CGFloat maxHeight = 500;
NSString *text = @"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe";
NSMutableString *tmpText = [[NSMutableString alloc] initWithString:text];
NSRange range = NSMakeRange([tmpText length] - 1, 1);
while ([text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > maxHeight) {
    [tmpText deleteCharactersInRange:range];
    range.location--;
}
NSLog(@"result: %@", tmpText);
[tmpText release];

I think this can do the job. It is not fully tested, but it works.

Upvotes: 3

Related Questions